Skip to content

Instantly share code, notes, and snippets.

@erikerlandson
Created March 29, 2021 22:28
Show Gist options
  • Save erikerlandson/f545b66c2f72c1f21d5980276764ecfb to your computer and use it in GitHub Desktop.
Save erikerlandson/f545b66c2f72c1f21d5980276764ecfb to your computer and use it in GitHub Desktop.
package repro
trait ResZero[V]:
type Res
object ResZero:
transparent inline given [V]: ResZero[V] = ${ macros.resZeroImpl[V] }
end ResZero
trait SomeTrait[V]:
type Res
object SomeTrait:
// what if I try to generalize a macro that sets any type with refinement 'type Res' to zero?
// fails to compile (see below)
//transparent inline given [V]: SomeTrait[V] = ${ macros.resZeroHKImpl[V, SomeTrait] }
end SomeTrait
object macros:
import scala.quoted.*
// compiles and works fine
def resZeroImpl[V](using Type[V], Quotes): Expr[ResZero[V]] =
'{ new _root_.repro.ResZero[V] { type Res = 0 } }
// try to define a macro that sets higher kinded type:
//def resZeroHKImpl[V, F[_] <: { type Res }](using Type[V], Type[F], Quotes): Expr[F[V]] =
// '{new F[V] { type Res = 0 } }
// the above fails to compile with error:
// [error] 20 | '{new F[V] { type Res = 0 } }
// [error] | ^^^^
// [error] |F[V] is not a class type
end macros
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment