2012-06-13 20 views
27

के साथ साथी ऑब्जेक्ट उदाहरण प्राप्त करें, क्या क्लास के साथी ऑब्जेक्ट का संदर्भ प्राप्त करना संभव है? मैं इन पंक्तियों के साथ कुछ सोच रहा हूं:स्कैला के नए प्रतिबिंब एपीआई के साथ, नए स्कैला प्रतिबिंब एपीआई

trait Base { 
    def companion: MetaBase = someReflectionMagic(this).asInstanceOf[MetaBase] 
} 

trait MetaBase { 
    // stuff 
} 

// --- 

class Foo extends Base 

object Foo extends MetaBase 

assert(new Foo.companion == Foo) 
+0

मुझे लगता है कि आप इस तरह से कुछ पाने में सक्षम हो सकते हैं http://stackoverflow.com/questions/1913092/getting-object-instance-by-string-name-in-scala – Noah

+3

मैं कुछ की उम्मीद कर रहा था नया प्रतिबिंब एपीआई का उपयोग कर थोड़ा क्लीनर। – leedm777

उत्तर

37

डेव! नए प्रतिबिंब में रुचि रखने के लिए धन्यवाद। शुरुआती गोद लेने वालों ने प्रतिबिंब और मैक्रोज़ की विकास प्रक्रिया को काफी हद तक प्रेरित किया है, और मैं अपने अद्भुत समुदाय का हिस्सा होने के बारे में बहुत खुश हूं।

अपने प्रश्न का उत्तर देने से पहले, मैं एक अस्वीकरण से शुरुआत करना चाहता हूं। 2.10.0-एम 4 में हमने अभी स्काला प्रतिबिंब एपीआई की नींव रखी है। यह अभी भी प्रेस से गर्म है, इसलिए दस्तावेज़ बहुत दुर्लभ हैं और एपीआई बिल्कुल सुविधा के साथ नहीं है। यह काम करता है, लेकिन इसके लिए परीक्षण और प्रतिक्रिया की आवश्यकता होती है। निश्चित रूप से, प्री-रिलीज एपीआई के साथ गड़बड़ परेशानी है, लेकिन मैं हमेशा मदद करने के लिए यहां हूं।

अब तक हमारे पास एक मसौदा है कि भविष्य में क्या प्रतिबिंब एसआईपी: https://docs.google.com/document/d/1Z1VhhNPplbUpaZPIYdc0_EUv5RiGQ2X4oqp0i-vz1qw/edit#heading=h.pqwdkl1226tc बन जाएगा। आप इसे तुरंत पढ़ सकते हैं, या पहले नीचे दिए गए मेरे उत्तर के माध्यम से स्किम कर सकते हैं।

trait Base { 
    def companion: MetaBase = { 
    // runtime reflection is typically done 
    // by importing things from scala.reflect.runtime package 
    import scala.reflect.runtime._ 

    // the new Scala reflection API is mirror based 
    // mirrors constitute a hierarchy of objects 
    // that closely follows the hierarchy of the things they reflect 
    // for example, for a class you'll have a ClassMirror 
    // for a method you'll have a MethodMirror and so on 
    // why go the extra mile? 
    // because this provides more flexibility than traditional approaches 
    // you can read more about mirror-based designs here: 
    // https://dl.dropbox.com/u/10497693/Library/Computer%20Science/Metaprogramming/Reflection/mirrors.pdf 
    // https://dl.dropbox.com/u/10497693/Library/Computer%20Science/Metaprogramming/Reflection/reflecting-scala.pdf 

    // bottom line is that to do anything you will need a mirror 
    // for example, in your case, you need a ClassMirror 

    // remember I said that mirrors provide more flexibility? 
    // for one, this means that mirror-based reflection facilities 
    // might have multiple implementations 
    // in a paper linked above, Gilad Bracha muses over a runtime 
    // that loads things remotely over the network 
    // in our case we might have different mirrors for JVM and CLR 
    // well, anyways 

    // the canonical (and the only one now) implementation of the mirror API 
    // is Java-based reflection that uses out of the box classloaders 
    // here's its root: https://github.com/scalamacros/kepler/blob/9f71e9f114c10b52350c6c4ec757159f06e55daa/src/reflect/scala/reflect/api/Mirrors.scala#L178 
    // yeah, right, I've just linked a source file from trunk 
    // we'll have Scaladocs for that soon, but for now take a look 
    // this file is interfaces-only and is heavy on comments 

    // to start with Java-based reflection implementation you need a classloader 
    // let's grab one and instantiate the root mirror 
    // btw, the same effect could be achieved by writing 
    // `scala.reflect.runtime.currentMirror` 
    val rootMirror = universe.runtimeMirror(getClass.getClassLoader) 

    // now when we've finally entered the reflective world 
    // we can get the stuff done 
    // first we obtain a ClassSymbol that corresponds to the current instance 
    // (ClassSymbols are to Scala the same as Classes are to Java) 
    var classSymbol = rootMirror.classSymbol(getClass) 

    // having a Scala reflection entity 
    // we can obtain its reflection using the rootMirror 
    val classMirror = rootMirror.reflectClass(classSymbol) 

    // now we just traverse the conceptual hierarchy of mirrors 
    // that closely follows the hierarchy of Scala reflection concepts 
    // for example, a ClassMirror has a companion ModuleMirror and vice versa 
    val moduleMirror = classMirror.companion.get 

    // finally, we've arrived at our destination 
    moduleMirror.instance.asInstanceOf[MetaBase] 
    } 
} 

trait MetaBase { 
    // stuff 
} 

// --- 

class Foo extends Base 

object Foo extends MetaBase 

object Test extends App { 
    assert(new Foo().companion == Foo) 
} 

अद्यतन। कृपया डैनियल सोब्राल द्वारा उत्कृष्ट पोस्ट भी देखें: http://dcsobral.blogspot.ch/2012/07/json-serialization-with-reflection-in.html

+0

[इस सवाल] को देखो (http://stackoverflow.com/q/10893712/53013) साथ ही - हालांकि वहां एक उत्तर है जो ठीक लगता है। –

+0

स्कैला 2.10.0 फ़ाइनल में काम करने के लिए इस कोड को कैसे अपडेट करें? –

+3

https://gist.github.com/xeno-by/4985929 –

5

मुझे यूजीन की आखिरी टिप्पणी नहीं मिली और इसके साथ आया। यह स्कैला 2.10 के लिए काम करता है।

trait ReflectionSugars{ 
    import scala.reflect.runtime.{universe => ru} 
    private lazy val universeMirror = ru.runtimeMirror(getClass.getClassLoader) 

    def companionOf[T](implicit tt: ru.TypeTag[T]) = { 
    val companionMirror = universeMirror.reflectModule(ru.typeOf[T].typeSymbol.companionSymbol.asModule) 
    companionMirror.instance 
    } 

} 

trait X extends ReflectionSugars{ 
    def companion = companionOf[X] 
} 

https://gist.github.com/piotrga/5928581

मुझे आशा है कि इस मदद करता है!

संबंधित मुद्दे