简体   繁体   English

将类型投影应用于精炼类型

[英]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现在我想写一个 function 大致看起来像

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完全可以像这样写一个 function

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} .但我想对T#TT2添加更严格的限制,使其成为精炼的类型T#TT2{ type TT4 = Int}

Is there any workaround?有什么解决方法吗?

Try wrapping type T#TT2 { type TT4 = Int } in parenthesis before the final projection like so尝试在最终投影之前将 type T#TT2 { type TT4 = Int }包装在括号中,如下所示

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM