twiddles icon indicating copy to clipboard operation
twiddles copied to clipboard

Rewrite twiddle syntax on Scala 3 as extension methods

Open mpilquist opened this issue 2 years ago • 2 comments

This was partially done in #9 but I was unable to get all examples compiling. The goal is to remove these implicit conversions: https://github.com/typelevel/twiddles/blob/7a26b389b9a8e638b06a1a8041f727905975aee0/core/shared/src/main/scala-3/org/typelevel/twiddles/Twiddles.scala#L38-L41 and the associated AnyVals.

See #11 for a potential starting point.

mpilquist avatar Oct 28 '23 12:10 mpilquist

Note part of #9 had to be rolled back due to #19

mpilquist avatar Jan 23 '24 17:01 mpilquist

I recently ran into the same problem as in #19 but needed a solution that works with extension methods. It's not pretty, but it works, so I'm just leaving this here as trivia:

Not working, breaks type inference (see #19):

extension [A](self: Self[A])
    def imap[B](f: A => B)(g: B => A): Self[B]
    def to[B](using convert: Convert[A, B]): Self[B] = imap(convert.to)(convert.from)

Surprisingly working, but the additional parentheses were a deal-breaker for me:

extension [A](self: Self[A])
    def imap[B](f: A => B)(g: B => A): Self[B]
    def to[B]()(using convert: Convert[A, B]): Self[B] = imap(convert.to)(convert.from)

Working drop-in replacement:

extension [A](self: Self[A])
    def imap[B](f: A => B)(g: B => A): Self[B]
    inline def to[B]: Self[B] =
      val convert = summonInline[Convert[A, B]]
      imap(convert.to)(convert.from)

taig avatar Apr 27 '25 11:04 taig