-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversion
behave differently depending on if implicit def
or given
is used
#21757
Comments
Your expectation is wrong. There are differences in behaviour that were introduced alongside the keyword change. |
wow I'm sorry this is new to me, let me try to find some old discussion to figure out the differences ... Automatic code search algorithms are complex subject, many implementations (sledgehammer in Isabelle/HOL, AlphaProof & LeanAgent in Lean 4) are prototypes and non-deterministic. To be used in Scala production code, users should be informed precisely about its capabilities and boundaries |
Strange, all the articles and discussion I found appears to suggest that the difference between "given" and "implicit" are purely syntactical (e.g. given permits anonymous instance). One authoritative document (https://docs.scala-lang.org/scala3/reference/changed-features/implicit-resolution.html) even suggest that search algorithms of both are upgraded simultaneously:
I'll try next to compile with Scala 3.5-migration option to try to emit more information (if applicable). But if not applicable, where can I find these differences in behaviour from the source code or a release note? |
These examples are quite confusing. First you define |
But how can I remove type parameters there? they are used in both return type and conversion body. |
Simply like that object OldStyle {
class A(val value: Int)
class B(val a: A)
class C(val b: B)
trait Conv[A, B] extends Conversion[A, B]
given ab: Conv[A, B] = { (a: A) => new B(a) }
given bc: Conv[B, C] = { (b: B) => new C(b) }
object ConvUtils {
implicit def hypotheticalSyllogism(
using
ab: Conv[A, B],
bc: Conv[B, C]
): Conv[A, C] = {
new Conv[A, C] {
def apply(a: A): C = bc(ab(a))
}
}
}
import ConvUtils.given
def demo(): Unit = {
val a = new A(42)
val c: C = a
println(c.b.a.value) // Outputs: 42
}
} Now |
Ad for |
thanks a lot @smarter ! |
I guess the hard way to answer the question of the differences between implicits and givens can look at the usage of |
Compiler version
3.5.1
Minimized code
The following 2 examples demonstrating hypothetical syllogism are almost identical, but the second failed, because
implicit def
is used instead ofgiven
(example 1)
(example 2)
Output
the error of example 2 is:
Expectation
despite the deprecation warning of
implicit
keyword, the 2 examples should use the same algorithm and behave identicallyThe text was updated successfully, but these errors were encountered: