2012-03-13 11 views
6

स्केलैडोक में, BitSet को Set[Int] विस्तारित करने के रूप में परिभाषित किया गया है। तो मैं एक BitSetSet[Int] के कहने काम करेगा में के रूप में प्रयोग में सोचा है, लेकिन मैं एक प्रकार मेल नहीं खाता मिलती है:बिटसेट को एक स्पष्ट कलाकार को सेट [Int] के उदाहरण के रूप में क्यों माना जाता है?

Welcome to Scala version 2.9.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_29). 

scala> import collection.BitSet 
import collection.BitSet 

scala> val b: Set[Int] = BitSet() 
<console>:8: error: type mismatch; 
found : scala.collection.BitSet 
required: Set[Int] 
     val b: Set[Int] = BitSet() 
          ^

हालांकि एक डाली काम करता है:

scala> val b: Set[Int] = BitSet().asInstanceOf[Set[Int]] 
b: Set[Int] = BitSet() 

तो क्यों मैं क्या ज़रूरत है करने के लिए स्पष्ट रूप से एक BitSet डाली Set[Int] पर जबकि Set[Int]Set[Int] का एक सुपर-प्रकार है?

उत्तर

9

यह पता चला है कि आपका Set वास्तव में scala.collection.immutable.Set है। आप

val b0: Set[Int] = collection.immutable.BitSet() 
val b1: collection.Set[Int] = collection.BitSet() 
val b2: collection.immutable.Set[Int] = collection.immutable.BitSet() 
val b3: collection.mutable.Set[Int] = collection.mutable.BitSet() 
val b4: collection.Set[Int] = collection.immutable.BitSet() 
val b5: collection.Set[Int] = collection.mutable.BitSet() 

लेकिन

val x1: collection.immutable.Set[Int] = collection.BitSet() 
val x2: collection.immutable.Set[Int] = collection.mutable.BitSet() 
val x3: collection.mutable.Set[Int] = collection.BitSet() 
val x4: collection.mutable.Set[Int] = collection.immutable.BitSet() 

की नहीं किसी भी कर सकते हैं तो और यह पता चला है कि Set के लिए डिफ़ॉल्ट आयात आप x2 देता है। collection.immutable.BitSet आयात करें, या collection.Set आयात करें (collection.immutable.Set को कवर करने के लिए)।

+0

@jullybobble - आप अभी भी उलझन में हैं। 'संग्रह .et' और' scala.collection.Set' _exactly वही चीज़_ हैं। आपका मतलब है 'प्रीड' से 'सेट'। –

+0

ओह, ठीक है! अब मैं समझ गया! सुधारों के लिए धन्यवाद! –

+0

नोट: मैंने अपनी पहली टिप्पणी हटा दी जिस पर @Rex ने उत्तर दिया क्योंकि यह गलत और भ्रामक था। –

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