-1
我有下面的代码片段:无形:隐函数体
val reprEncoder: CsvEncoder[String :: Int :: Boolean :: HNil] =
implicitly
是什么意思implicitly
这里?
我有下面的代码片段:无形:隐函数体
val reprEncoder: CsvEncoder[String :: Int :: Boolean :: HNil] =
implicitly
是什么意思implicitly
这里?
它的意思是:“召唤你的范围内的暗示实例为CsvEncoder[String :: Int :: Boolean :: HNil]
”。下面简单的例子,在Scala的REPL会话,应该清楚:
$ scala
Welcome to Scala 2.12.3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_144).
Type in expressions for evaluation. Or try :help.
scala> implicit val str: String = "hello"
str: String = hello
scala> val anotherStr: String = implicitly
anotherStr: String = hello
正如你可以看到分配给anotherStr
值是,str
这是在范围String
类型的唯一的内含价值。请注意,如果在范围编译中有多个相同类型的隐式值,将会失败并显示错误:“模糊隐式值”。确实如下:
scala> implicit val str: String = "hello"
str: String = hello
scala> implicit val str2: String = "world"
str2: String = world
scala> val anotherStr: String = implicitly
<console>:16: error: ambiguous implicit values:
both value StringCanBuildFrom in object Predef of type =>
scala.collection.generic.CanBuildFrom[String,Char,String]
and method $conforms in object Predef of type [A]=> A <:< A
match expected type T
val anotherStr: String = implicitly
^
scala>
请问在请求文档之前 – cchantep
我看了看,但无法配置它。那是因为我在这里问。 –
'隐式地'定义在这里:https://github.com/scala/scala/blob/2.12.x/src/library/scala/Predef.scala#L187 –