Simple application of opaque types, extension methods hangs in runtime
See original GitHub issueCompiler version
3.0.0-M3
Minimized code
as scastie: https://scastie.scala-lang.org/3YbjmeaUTYWzwBOQOdRt8g
trait Eq[A]:
def eqv(self: A, another: A): Boolean
extension (self: A) def ===(another: A): Boolean = eqv(self, another)
object Eq:
def apply[T: Eq]: Eq[T] = summon[Eq[T]]
given Eq[String] = (self, another) => self == another
opaque type UserId = String
object UserId {
def apply(value: String): UserId = value
given Eq[UserId] =
println("bar")
Eq[String]
}
object Main:
def main(args: Array[String]): Unit =
println(UserId("foo") === UserId("foo"))
Output
bar
<hangs now>
Expectation
outputs bar
, then true
, then exits.
More context
This doesn’t happen when Eq[UserId].eqv(..., ...)
is used instead.
Issue Analytics
- State:
- Created 3 years ago
- Comments:13 (12 by maintainers)
Top Results From Across the Web
SIP-35 - Opaque types - Scala Documentation
Introduction. This is a proposal to introduce syntax for type aliases that only exist at compile time and emulate wrapper types.
Read more >Paying homage to the Compall-Michael pattern, and ...
In a nutshell, we want to have "compile-time wrapper types" that provide type-safety at compile-time but without runtime overhead (e.g. no extra ...
Read more >scala - Top level opaque type with same underlying type and ...
I ran into an issue when defining opaque types with identical underlying types and extension methods, e.g.: opaque type HandleA = Int object ......
Read more >swift-evolution/0244-opaque-result-types.md at main - GitHub
In other words, like generic arguments, opaque result types are only opaque to the static type system. They don't have an independent existence...
Read more >Opaque Type Aliases - Scala 3 - EPFL
Opaque types aliases provide type abstraction without any overhead. ... None end Logarithm // Extension methods define opaque types' public APIs extension ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Yep! (but before adding complexity to the language, it’s also worth asking first if
case class UserId(value: String) derives Eq
isn’t good enough, sure you get extra allocations but I’m not convinced that is a big deal: https://contributors.scala-lang.org/t/synthesize-constructor-for-opaque-types/4616/76?u=smarter)@kubukoz duplicate of https://github.com/lampepfl/dotty/issues/10947