相关疑难解决方法(0)

如何避免在 Scala 中调用 asInstanceOf

这是我的代码的简化版本。

我怎样才能避免打电话asInstanceOf(因为这是设计不佳的解决方案的味道)?

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 { …
Run Code Online (Sandbox Code Playgroud)

casting scala implicit pattern-matching typeclass

6
推荐指数
1
解决办法
128
查看次数

标签 统计

casting ×1

implicit ×1

pattern-matching ×1

scala ×1

typeclass ×1