简体   繁体   English

Scala隐式上下文解析

[英]Scala implicit context resolution

I have found there rules for implicict resolution i SLS: 我发现有用于SLS隐式解决规则的规则:

  1. if T is a compound type T1 with ... with Tn, the union of the parts of T1, ..., Tn, as well as T itself 如果T是具有...和Tn的复合类型T1,则T1,...,Tn以及T本身的部分的并集

  2. if T is a parameterized type S[T1, ..., Tn], the union of the parts of S and T1, ..., Tn 如果T是参数化类型S [T1,...,Tn],则S和T1,...,Tn的部分的并集

  3. if T is a singleton type p.type, the parts of the type of p 如果T是单例类型p.type,则p类型的部分

  4. if T is a type projection S#U, the parts of S as well as T itself 如果T是类型投影S#U,则S的部分以及T本身

  5. in all other cases, just T itself 在所有其他情况下,仅T本身

Is example below implicit resolution based on rule 4? 下面的示例是否基于规则4隐式解决?

object Foo{
   trait Bar
   implicit def newBar = new Bar{
        override def toString = "Implicit Bar"
   }

}

implicitly[Foo.Bar]

Thanks 谢谢

Zlaja Zlaja

Yes, I believe that is correct. 是的,我相信这是正确的。 I think for a singleton object Foo , type Foo.Bar is the same as Foo.type#Bar : 我认为对于单例对象Foo ,类型Foo.BarFoo.type#Bar相同:

implicitly[Foo.type#Bar] // ok

Also: 也:

def a(f: Foo.type#Bar) {}
def b(f: Foo.Bar) { a(f) }       // accepted

def c(f: Foo.Bar) {}
def d(f: Foo.type#Bar) { c(f) }  // accepted

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

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