【问题标题】:Path-dependent types and nested traits路径依赖类型和嵌套特征
【发布时间】:2013-03-20 06:38:08
【问题描述】:

背景

假设我有一些嵌套的特征:

trait Foo { trait Bar }

还有几个例子:

val myFoo = new Foo {}
val myBar = new myFoo.Bar {}

我可以编写以下内容,看起来(至少乍一看)他们应该或多或少做同样的事情:

def whatever1(foo: Foo)(bar: foo.Bar) = bar
def whatever2(foo: Foo): foo.Bar => foo.Bar = { bar => bar }
def whatever3(foo: Foo) = new { def apply(bar: foo.Bar) = bar }
case class whatever4(foo: Foo) { def apply(bar: foo.Bar) = bar }
case class whatever5[F <: Foo](foo: F) { def apply(bar: foo.Bar) = bar }

请注意,最后一个是受到here 给出的解决方案的启发。

前三部作品:

scala> val sameBar1: myFoo.Bar = whatever1(myFoo)(myBar)
sameBar1: myFoo.Bar = $anon$1@522f63e7

scala> val sameBar2: myFoo.Bar = whatever2(myFoo)(myBar)
sameBar1: myFoo.Bar = $anon$1@522f63e7

scala> val sameBar3: myFoo.Bar = whatever3(myFoo)(myBar)
sameBar2: myFoo.Bar = $anon$1@522f63e7

但不是第四个或第五个:

scala> val sameBar4: myFoo.Bar = whatever4(myFoo)(myBar)
<console>:12: error: type mismatch;
 found   : myFoo.Bar
 required: _1.foo.Bar where val _1: whatever4
       val sameBar4: myFoo.Bar = whatever4(myFoo)(myBar)
                                                  ^

很公平——我们也不能这样做,可能出于类似的原因:

scala> val myOof = myFoo
myOof: Foo = $anon$1@39e4ff0c

scala> val myOofBar: myOof.Bar = new myFoo.Bar {}
<console>:10: error: type mismatch;
 found   : myFoo.Bar
 required: myOof.Bar
       val myOofBar: myOof.Bar = new myFoo.Bar {}
                                 ^

这没什么大不了的,因为我们有三个可行的解决方案。

问题

(我首先要指出的是,虽然我在使用宏时第一次遇到下面的问题,虽然我这里的示例涉及反射 API,但我的问题并不特定于宏或反射。)

假设我正在使用新的反射 API 并希望能够编写以下内容:

applier[List[_]](Literal(Constant(42)), Literal(Constant(13)))

它的意思是“给我List(42, 13) 的抽象语法树”。这并不难——我可以使用上面whatever3 的方法:

trait ReflectionUtils {
  import scala.reflect.api.Universe
 
  def companionApplier(u: Universe) = new {
    def apply[A: u.TypeTag](xs: u.Tree*): u.Tree = u.Apply(
      u.Select(u.Ident(u.typeOf[A].typeSymbol.companionSymbol), "apply"),
      xs.toList
    )
  }
}

现在我在我的宏中获得了我想要的语法(请参阅我的 answerthis question 以获得更详细和更有动力的示例):

object MacroExample extends ReflectionUtils {
  import scala.language.experimental.macros
  import scala.language.reflectiveCalls
  import scala.reflect.macros.Context

  def threeOfThem(n: Int) = macro threeOfThem_impl
  def threeOfThem_impl(c: Context)(n: c.Expr[Int]) = {
    val applier = companionApplier(c.universe)

    c.Expr[List[Int]](applier[List[_]](n.tree, n.tree, n.tree))
  }
}

一切都按预期进行。不过,我不太喜欢“结构类型成员的反射访问”业务。不幸的是,我不能在这里使用whatever1whatever2 方法,因为当我将这个东西应用于我的宇宙时,我无法修复类型参数。我希望能够写出以下内容:

case class companionApplier(u: Universe) {
  def apply[A: u.TypeTag](xs: u.Tree*): u.Tree = u.Apply(
    u.Select(u.Ident(u.typeOf[A].typeSymbol.companionSymbol), "apply"),
    xs.toList
  )
}

但这当然让我遇到了我们在上面看到的whatever4 的类型不匹配问题。

我还缺少其他技巧吗?我是否有可能在不使用具有结构类型成员的匿名类的情况下获得我想要的语法?

【问题讨论】:

    标签: scala types nested-class path-dependent-type dependent-method-type


    【解决方案1】:

    这应该可行:

    case class companionApplier[U <: Universe](u: U) { ... }
    
    // in macro
    companionApplier[c.universe.type](c.universe)
    

    几个月前我也有类似的问题,请参阅here

    【讨论】:

    • 我也这么认为(这是我的whatever5 中的方法),但不幸的是,它没有——您仍然会遇到类型不匹配。我会更新问题。
    • 哦,没关系——一旦我添加了显式类型参数,它似乎确实有效。我没有尝试过这种变化,因为在迈尔斯的解决方案中没有必要。谢谢!
    【解决方案2】:

    如何将结构类型拆分为辅助类型,然后从列表中添加一些我的解决方案,

    scala> trait Foo { trait Bar }
    defined trait Foo
    
    scala> val myFoo = new Foo {} ; val myBar = new myFoo.Bar {}
    myFoo: Foo = $anon$1@11247416
    myBar: myFoo.Bar = $anon$2@70415924
    
    scala> class Whatever6Aux[F <: Foo](val foo: F) { def apply(bar: foo.Bar) = bar }
    defined class Whatever6Aux
    
    scala> def whatever6(foo: Foo) = new Whatever6Aux[foo.type](foo)
    whatever6: (foo: Foo)Whatever6Aux[foo.type]
    
    scala> import scala.language.existentials
    import scala.language.existentials
    
    scala> whatever6(myFoo)(myBar)
    res0: _1.foo.Bar forSome { val _1: Whatever6Aux[<refinement>.type] } = $anon$2@70415924
    

    【讨论】:

      猜你喜欢
      • 2013-06-20
      • 2015-09-21
      • 2015-12-23
      • 1970-01-01
      • 2019-06-06
      • 1970-01-01
      • 1970-01-01
      • 2019-06-03
      • 1970-01-01
      相关资源
      最近更新 更多