作为类型成员的单例类型
Singleton type as a type member
我正在尝试将类型成员定义为单例类型。这是我的尝试:
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 }
有没有办法将类型成员设置为单例类型(Boolean(false)
在我的例子中)?
def f(t: Test{ type Blocked = false }) = ??? // argument is parsed as t: Test{ type Blocked = Boolean }
Blocked
变成文字单例类型 false
;请注意 f(true)
是如何被拒绝的
scala> trait Test{
| type Blocked <: Boolean
| }
trait Test
scala> val v = new Test { type Blocked = false }
val v: Test{type Blocked = false} = $anon@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
我正在尝试将类型成员定义为单例类型。这是我的尝试:
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 }
有没有办法将类型成员设置为单例类型(Boolean(false)
在我的例子中)?
def f(t: Test{ type Blocked = false }) = ??? // argument is parsed as t: Test{ type Blocked = Boolean }
Blocked
变成文字单例类型 false
;请注意 f(true)
是如何被拒绝的
scala> trait Test{
| type Blocked <: Boolean
| }
trait Test
scala> val v = new Test { type Blocked = false }
val v: Test{type Blocked = false} = $anon@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