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?

Mario Galic
  • 47,285
  • 6
  • 56
  • 98
  • 1
    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. – user Apr 13 '21 at 18:17
  • 2
    As stated [here](https://contributors.scala-lang.org/t/proposal-to-add-kind-polymorphism-to-the-language/2958): *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.* – gianluca aguzzi May 02 '21 at 21:26
  • 1
    https://stackoverflow.com/questions/75494119/scala-3-kind-polymorphism-and-anykind-type-any-code-example – Dmytro Mitin Mar 23 '23 at 22:38

0 Answers0