scalashapelesstype-safetyrefined

Singleton type as a type member


I'm trying to define a type member to be a singleton type. Here is my attempt:

import shapeless.syntax.singleton._

trait Test{
    type Blocked <: Boolean
}

def f(t: Test{ type Blocked = false.narrow }) = ??? // does not compile

def f(t: Test{ type Blocked = false }) = ??? // argument is parsed as t: Test{ type Blocked = Boolean }

Is there a way to set a type member to a singleton type (Boolean(false) in my case)?


Solution

  • def f(t: Test{ type Blocked = false }) = ??? // argument is parsed as t: Test{ type Blocked = Boolean }

    Blocked becomes literal singleton type false; note how f(true) is rejected bellow

    scala> trait Test{
         |     type Blocked <: Boolean
         | }
    trait Test
    
    scala> val v = new Test { type Blocked = false }
    val v: Test{type Blocked = false} = $anon$1@34aa8b61
    
    scala> def f(x: v.Blocked) = x
    def f(x: v.Blocked): v.Blocked
    
    scala> f(false)
    val res0: v.Blocked = false
    
    scala> f(true)
             ^
           error: type mismatch;
            found   : Boolean(true)
            required: v.Blocked
               (which expands to)  false