Scala:获取字符串转换的通用函数

Scala: generic function to get a String converted

我有一张带有 getter 方法的地图。键始终是字符串,值是任意。 我想让调用者使用如下方法

get[Int](k: String)
get[Boolean](k:String)

并在此方法内将字符串转换为用户指定的特定类型。我想到的直接解决方案是

def get[T](k: String): T = k.asInstanceOf[T]

这是行不通的。然后我尝试

def cast[T](x: String, classTag: ClassTag[T]): T = classTag match {
   case Int => x.toInt
   case Boolean => x.toBoolean
   ...
}  

不编译。我不确定这是否可能。任何想法或者我需要编写我想要的所有方法?例如

def getInt(k: String): Int
def getBoolean(k: String): Boolean
...

这是 scala 中广泛使用的类型类模式的经典用例。我假设您有 Mapget 方法的自定义实现。

trait Converter[T]{        // typeclass
  def convert(t:String):T
}

implicit object ToIntConverter extends  Converter[Int] {
  def convert(t:String):Int = t.toInt
}


implicit object ToBooleanConverter extends  Converter[Boolean] {
  def convert(t:String):Boolean = t.toBoolean
}

//       vvv approach bellow works starting from scala 2.12 vvv
//
// implicit val ToBooleanConverter: Converter[Boolean] = s => s.toBoolean 
// implicit val ToIntConverter : Converter[Int]  = s => s.toInt 


def get[T](k:String)(implicit cv: Converter[T]):T= cv.convert(k)


println(get[Int]("1"))
println(get[Boolean]("true"))

我得到了下面的工作。

val anyMap: Map[String, Any] = Map(
  "a" -> 1,
  "b" -> true
)

def getInst[T](amap: Map[String, Any])(k: String): T = amap.get(k) match {
  case Some(thing) => thing.asInstanceOf[T]
  case None => throw new IllegalArgumentException
}

getInst[Int](anyMap)("a")
getInst[Boolean](anyMap)("b")

像 Map[String, Any] 这样的东西不太安全,因为转换可能会失败。可能最好在您的地图中引入一些临时多态性(不确定)。