Yeah, you can explicitly pass an implicit parameter (Scala 3 will make this even more explicit), but here you want to a non-implicit value to be picked for an implicit value, that doesn’t work never (and for good reasons).
Also, it is important to understand that there is a difference between implicit def foo(bar: Bar): Foo
and implicit def foo(implicit bar: Bar): Foo
, the first one is an implicit conversion from Bar
to Foo
, the second one is an inductive derivation of a Foo
given a Bar
(quite like logic programming) that why usually this version is parametrized on at least one type parameter.
Being honest, I would say that in this case, it should actually have been an implicit conversion, but there are probably reasons for it being an induction; but it would be good if someone that worked in the stdlib would complement on this.
Also, about the compiler picking a non-implicit value for an implicit argument would not only make the implicit resolution more complex and slower but also more fragile given there could be many possible values.
makes the call sorted(String.CASE_INSENSITIVE_ORDER)
work.
Yeah because you used an implicit conversion, which as I said I also agree with you it would be better.
To be honest, I guess we all that have had this problem thought the same.
Not sure if Scala 3 improves on this? It is also questionable if there is any use case for a recursive implicit but knowing this community probably there is at least one.
If your value is lazy it can be recursive without the val
being lazy, example:
val fibs: LazyList[BigInt] = BigInt(0) #:: BigInt(1) #:: fibs.zip(fibs.tail).map(n => n._1 + n._2)
I believe that since 2.13
there is a compiler flag that catches these errors, I do not remember which one it was though; since I am too used to sbt-tpolecat to turn all the useful flags for me.
Hope I helped with that
You can also:
implicit val caseInsensitive: Ordering[String] = Ordering.fromLessThan {
(x, y) => x.compareToIgnoreCase(y) < 0
}
Yeah, I guess you either have to be completely explicit about this or use your own implicit conversion as you did.