How to avoid calling asInstanceOf in Scala
Asked Answered
S

1

6

Here is a simplified version of my code.

How can I avoid to call asInstanceOf (because it is a smell for a poorly design solution) ?

sealed trait Location
final case class Single(bucket: String)     extends Location
final case class Multi(buckets: Seq[String]) extends Location

@SuppressWarnings(Array("org.wartremover.warts.AsInstanceOf"))
class Log[L <: Location](location: L, path: String) { // I prefer composition over inheritance
  // I don't want to pass location to this method because it's a property of the object
  // It's a separated function because there is another caller
  private def getSinglePath()(implicit ev: L <:< Single): String = s"fs://${location.bucket}/$path"

  def getPaths(): Seq[String] =
    location match {
      case _: Single => Seq(this.asInstanceOf[Log[_ <: Single]].getSinglePath())
      case m: Multi  => m.buckets.map(bucket => s"fs://${bucket}/$path")
    }
}
Scuff answered 3/7, 2020 at 13:20 Comment(0)
P
7

Try a type class

class Log[L <: Location](location: L, val path: String) {
  def getSinglePath()(implicit ev: L <:< Single): String = s"fs://${location.bucket}/$path"
  def getPaths()(implicit gp: GetPaths[L]): Seq[String] = gp.getPaths(location, this)
}

trait GetPaths[L <: Location] {
  def getPaths(location: L, log: Log[L]): Seq[String]
}
object GetPaths {
  implicit val single: GetPaths[Single] = (_, log) => Seq(log.getSinglePath())
  implicit val multi:  GetPaths[Multi]  = (m, log) => m.buckets.map(bucket => s"fs://${bucket}/${log.path}")
}

Normally a type class is a compile-time replacement for pattern matching.

I had to make getSinglePath public and path a val in order to provide access to them inside GetPaths. If you don't want to do so you can make the type class nested into Log

class Log[L <: Location](location: L, path: String) {
  private def getSinglePath()(implicit ev: L <:< Single): String = s"fs://${location.bucket}/$path"
  def getPaths()(implicit gp: GetPaths[L]): Seq[String] = gp.getPaths(location)

  private trait GetPaths[L1 <: Location] {
    def getPaths(location: L1): Seq[String]
  }
  private object GetPaths {
    implicit def single(implicit ev: L <:< Single): GetPaths[L] = _ => Seq(getSinglePath())
    implicit val multi: GetPaths[Multi] = _.buckets.map(bucket => s"fs://${bucket}/$path")
  }
}

Actually we don't have to pass location explicitly and we don't need L1

class Log[L <: Location](location: L, path: String) {
  private def getSinglePath()(implicit ev: L <:< Single): String = s"fs://${location.bucket}/$path"
  def getPaths()(implicit gp: GetPaths): Seq[String] = gp.getPaths()

  private trait GetPaths {
    def getPaths(): Seq[String]
  }
  private object GetPaths {
    implicit def single(implicit ev: L <:< Single): GetPaths = () => Seq(getSinglePath())
    implicit def multi(implicit ev: L <:< Multi):   GetPaths = () => location.buckets.map(bucket => s"fs://${bucket}/$path")
  }
}

Now GetPaths is zero-parameter type class and slightly similar to magnet pattern.

Parthenon answered 3/7, 2020 at 13:34 Comment(2)
With TC, the location must be passed explictly, So there is a room for coding mistake : gp.getPaths(SingleBucket("nottheonefrommyobject") It doesn't seem a big win compared to private def getSinglePath(location: Single): String = s"fs://${location.bucket}/$path" Scuff
@YannMoisan You're right. We don't have to pass location explicitly. See update.Parthenon

© 2022 - 2024 — McMap. All rights reserved.