implicit-parameters

Can an implicit conversion of an implicit value satisfy an implicit parameter?

こ雲淡風輕ζ 提交于 2020-01-05 07:00:38
问题 I'm defining some Scala implicits to make working with a particular unchangeable set of Java classes easier. The following Scala code is a simplified example that obviously looks crazy, in the real world I'm trying to grab particular resources (rather than numeric age) implicitly from the Monkey, Tree & Duck for use in various methods like purchaseCandles() : // actually 3 Java classes I can not change: case class Monkey(bananas: Int) case class Tree(rings: Int) case class Duck(quacks: Seq

Implicit parameters and NullPointerException in Scala

可紊 提交于 2019-12-13 05:15:06
问题 I am trying to define a function that takes an integer and an implicit object that has the code to process that number, but i get a NullPointerException and i don know why. If I delete the first println the code works. Is there some problem with the way i am defining the implicit objects? Here is my code: class A { def apply(n : Int) = n*2 } def f(n: Int)(implicit o : A) = o(n) implicit val a = new A println(f(3)) class B extends A { override def apply(n: Int) = n*3 } implicit val b = new B

Scala apply method call as parentheses conflicts with implicit parameters

瘦欲@ 提交于 2019-12-10 16:48:41
问题 There is a note in Cay Horstmann's book "Scala for the Impatient" about the apply method: Occasionally, the () notation conflicts with another Scala feature: implicit parameters. For example, the expression "Bonjour".sorted(3) yields an error because the sorted method can optionally be called with an ordering, but 3 is not a valid ordering. The solution is to assign "Bonjour".sorted to a variable and call apply on it, for example: val result = "Bonjour".sorted result(3) Or call apply