3 回答

TA貢獻(xiàn)1921條經(jīng)驗(yàn) 獲得超9個(gè)贊
明智地包含諷刺!YMMV ...
路易吉的答案是完整而正確的。這只是通過舉例說明如何光榮地過度使用隱式實(shí)例來擴(kuò)展它,因?yàn)樗赟cala項(xiàng)目中經(jīng)常發(fā)生。實(shí)際上,您經(jīng)常甚至可以在“最佳實(shí)踐”指南之一中找到它。
object HelloWorld {
case class Text(content: String)
case class Prefix(text: String)
implicit def String2Text(content: String)(implicit prefix: Prefix) = {
Text(prefix.text + " " + content)
}
def printText(text: Text): Unit = {
println(text.content)
}
def main(args: Array[String]): Unit = {
printText("World!")
}
// Best to hide this line somewhere below a pile of completely unrelated code.
// Better yet, import its package from another distant place.
implicit val prefixLOL = Prefix("Hello")
}
添加回答
舉報(bào)