-
Notifications
You must be signed in to change notification settings - Fork 1.1k
How to synthesize a higher-kinded type parameter? #20000
-
I need to synthesize polymorphic function with a higher-kinded type parameter, such as
def fancyIdentity[F[_], A](fa: F[A]): F[A] // or val fancyIdentity: [F[_], A] => F[A] => F[A]
How do I create type for such method/function?
Note that PolyType
takes TypeBounds
:
/** Type of the definition of a method taking a list of type parameters. It's return type may be a MethodType. */ type PolyType <: MethodOrPoly val PolyType: PolyTypeModule trait PolyTypeModule { this: PolyType.type => def apply(paramNames: List[String])( paramBoundsExp: PolyType => List[TypeBounds], // How to specify the kind of F[_] as TypeBounds? resultTypeExp: PolyType => TypeRepr ): PolyType }
However, when I print out the tree structure of a higher-kinded type parameter, I get back a LambdaTypeTree
, not TypeBoundsTree
.
I am using Scala 3.4.0.
Beta Was this translation helpful? Give feedback.
All reactions
To answer my own question: higher-kinds can be represented as TypeBounds
with an upper bound of TypeLambda
.
For example,
TypeBounds( low = TypeRepr.of[Nothing], hi = TypeLambda( List("X"), _ => List(TypeBounds.empty), _ => TypeRepr.of[Any] ) )
are bounds of a type parameter F[_]
.
The resulting TypeBounds
can then be used in construction of PolyType
.
Replies: 1 comment
-
To answer my own question: higher-kinds can be represented as TypeBounds
with an upper bound of TypeLambda
.
For example,
TypeBounds( low = TypeRepr.of[Nothing], hi = TypeLambda( List("X"), _ => List(TypeBounds.empty), _ => TypeRepr.of[Any] ) )
are bounds of a type parameter F[_]
.
The resulting TypeBounds
can then be used in construction of PolyType
.
Beta Was this translation helpful? Give feedback.