Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to synthesize a higher-kinded type parameter? #20000

Discussion options

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.

You must be logged in to vote

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

Comment options

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.

You must be logged in to vote
0 replies
Answer selected by TomasMikula
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant

AltStyle によって変換されたページ (->オリジナル) /