Use case of kind polymorphism with AnyKind
Asked Answered
Y

0

9

Given

trait Int                // proper type
trait List[A]            // 1st-order-kinded type constructor 
trait Functor[F[_]]      // higher-order-kinded type constructor taking type constructor
trait I[H[F[_]]]         // higher-order-kinded type constructor taking higher-order type constructor that takes 1st-order type constructor

we cannot pass type argument of different kind as compared to the kind of declared type parameter

scala> def f[F[_[_[_]]]] = 42                                                                                                                                
def f[F[_$1]] => Int

scala> f[I]                                                                                                                                                  
val res5: Int = 42

scala> f[Functor]                                                                                                                                            
1 |f[Functor]
  |  ^
  |  Type argument Functor does not conform to upper bound [_$1[_$2]] =>> Any

however we can declare the type parameter to be polymorphic in its kind via AnyKind

scala> def f[A <: AnyKind] = 42                                                                                                       
def f[A <: AnyKind] => Int

scala> f[Int]
val res10: Int = 42

scala> f[List]                                                                                                                                               
val res11: Int = 42

scala> f[Functor]                                                                                                                                            
val res12: Int = 42

scala> f[I]                                                                                                                                                  
val res13: Int = 42

What is the use case of AnyKind? What practical problem does it solve?

Yost answered 13/4, 2021 at 16:3 Comment(3)
Well, it does allow one to implement the SKI combinator calculus at a type level, but I guess that's not a practical problem. It may be useful in match types.Suwannee
As stated here: the primary use of AnyKind is to index types and type classes that are naturally kind polymorphic, such as Type in the Scala 3 quote/splice based metaprogramming facility.Seligman
#75494619Anni

© 2022 - 2024 — McMap. All rights reserved.