2011-12-16 12 views
5

संभव डुप्लिकेट:
What does “outer =>” really mean?स्वयं => के लिए क्या उपयोग किया जाता है?

जब मैं के स्रोत कोड में देखो: स्केला/src/पुस्तकालय/स्केला/Option.scala

sealed abstract class Option[+A] extends Product with Serializable { 
    self => 

मैं भटकना स्वयं के लिए क्या उपयोग करते हैं। मैं जानता हूँ कि स्वयं प्रकार के सामान्य उपयोग के वर्ग विशेषता में मिलाया जा सकता है प्रतिबंधित करने के लिए है जैसे:।

scala> trait A 
defined trait A 

scala> trait NeedA {self: A =>} 
defined trait NeedA 

scala> new NeedA {} 
<console>:10: error: illegal inheritance; 
self-type java.lang.Object with NeedA does not conform to NeedA's selftype NeedA with A 
       new NeedA {} 
       ^

scala> new NeedA with A {} 
res39: java.lang.Object with NeedA with A = [email protected] 

scala> 

लेकिन "इस =>" नहीं मामला है। वास्तव में यह "यह =>" किस लिए उपयोग किया जाता है?

उत्तर

5

यह इस करने के लिए एक अन्य नाम है, जो भीतर की कक्षाओं में काम किया जा सकता है (OuterClass.this के लिए पर्याय बन गया)

class A {self => 
    ... 
    class B { 
     // self is the enclosing A, synonymous for A.this 
    } 
} 
बनाता है
संबंधित मुद्दे