question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Simple application of opaque types, extension methods hangs in runtime

See original GitHub issue

Compiler 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:closed
  • Created 3 years ago
  • Comments:13 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
smartercommented, Feb 17, 2021

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)

1reaction
Swoorupcommented, Feb 13, 2021
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found