scalatype-safetytype-projectionrefined

Apply type projection to a refined type


Consider the following example:

trait T3

trait T2{
  type TT4
  type TT3 <: T3
}

trait T1{
  type TT2 <: T2
}

now I want to write a function the roughly speaking looks as

def test[T <: T1](t: T#TT2{type TT4 = Int}#TT3) = println(t)  //invalid syntax

which unfortunately is not a valid syntax. It is perfectly possible to write a function like this

def test[T <: T1](t: T#TT2#TT3) = println(t)

But I'd like to add a bit stricter restriction on T#TT2 making it to be a refined type T#TT2{ type TT4 = Int}.

Is there any workaround?


Solution

  • Try wrapping type T#TT2 { type TT4 = Int } in parenthesis before the final projection like so

    def test[T <: T1](t: (T#TT2 { type TT4 = Int })#TT3) = ???
    

    Types can always be wrapped in parentheses

    SimpleType        ::=  SimpleType TypeArgs
                          |  SimpleType ‘#’ id
                          |  StableId
                          |  Path ‘.’ ‘type’
                          |  Literal
                          |  ‘(’ Types ‘)’      <======= note the parentheses
    

    for example

    scala> val xs: (List[(Int)]) = List(42)
    val xs: List[Int] = List(42)