scalascala-3higher-kinded-typestype-constructor

In Scala 3/Dotty, is it possible to write a kind/type constructor that takes a value with a unique path as an argument?


Here is a simple example of my intention:

  sealed trait Col[V] {

    trait Wrapper
  }

  object Col1 extends Col[Int]
  object Col2 extends Col[Double]

  type WOf[T <: Col[_] with Singleton] = T#Wrapper

this is an alternative (simpler & more generalisable) way of writing:

type WOf[T <: Col[_] with Singleton] = T match {
  case Col1.type => Col1.Wrapper
  case Col2.type => Col2.Wrapper
}

But the compiler won't let me:


[Error] 
***.scala:25:42: T is not a legal path
since it is not a concrete type

So what is the correct way to write the kind WOf?

UPDATE 1: I could speculate that Scala 3 can use this type constructor to represent an Eta-expanded polymorphic function of the following definition:

def wOf[T <: Col[_]](v: T): v.Wrapper = ???

I just don't know what it is, is there any reason Scala 3 will choose to make this kind definition only available for a very specific case?


Solution

  • Since this is a niche case of a new language, I would not expect to find example. So I end up asking BSP to generate it based on a variant of the official example:

    https://docs.scala-lang.org/scala3/reference/new-types/polymorphic-function-types.html

    class DependentPoly {
    
      sealed trait Col[V] {
    
        trait Wrapper
        val wrapper: Wrapper = ???
      }
    
      object Col1 extends Col[Int]
    
      object Col2 extends Col[Double]
    
      val polyFn = [C <: Col[?]] => (x: C) => x.wrapper
    }
    

    The type annotation generated by BSP is:

    [C <: Col[?]] => (x: C) => x.Wrapper 
    

    Unfortunately it triggered a compiler bug at this moment:

    https://github.com/lampepfl/dotty/issues/16756

    So we'll have to wait for it to be fixed before using it anywhere