2013-03-12 16 views
7
scala> val s: Seq[Class[_ <: java.lang.Enum[_]]] = Seq(classOf[java.util.concurrent.TimeUnit]) 
s: Seq[Class[_ <: java.lang.Enum[_]]] = List(class java.util.concurrent.TimeUnit) 

scala> s.flatMap(_.getEnumConstants) 
<console>:9: error: no type parameters for method flatMap: (f: Class[_ <: java.lang.Enum[_]] => scala.collection.GenTraversableOnce[B])(implicit bf: scala.collection.generic.CanBuildFrom[Seq[Class[_ <: java.lang.Enum[_]]],B,That])That exist so that it can be applied to arguments (Class[_ <: java.lang.Enum[_]] => scala.collection.mutable.ArrayOps[_$1(in value $anonfun) with java.lang.Object] forSome { type _$1(in value $anonfun) <: java.lang.Enum[_] }) 
--- because --- 
argument expression's type is not compatible with formal parameter type; 
found : Class[_ <: java.lang.Enum[_]] => scala.collection.mutable.ArrayOps[_$1(in value $anonfun) with java.lang.Object] forSome { type _$1(in value $anonfun) <: java.lang.Enum[_] } 
required: Class[_ <: java.lang.Enum[_]] => scala.collection.GenTr...    s.flatMap(_.getEnumConstants) 

उत्तर

6

नहीं वास्तव में सटीक जवाब के साथ प्राप्त कर सकते हैं, लेकिन दो टिप्पणियों - स्काला 2.10 आप दे देंगे एक अच्छे त्रुटि:

scala> s.flatMap(_.getEnumConstants) 
<console>:9: error: no type parameters for method flatMap: (f: Class[_ <: Enum[_]] => scala.collection.GenTraversableOnce[B])(implicit bf: scala.collection.generic.CanBuildFrom[Seq[Class[_ <: Enum[_]]],B,That])That exist so that it can be applied to arguments (Class[_ <: Enum[_]] => scala.collection.mutable.ArrayOps[(some other)_$1(in object $iw) with Object] forSome { type (some other)_$1(in object $iw) <: Enum[_] }) 
--- because --- 
argument expression's type is not compatible with formal parameter type; 
found : Class[_ <: Enum[_]] => scala.collection.mutable.ArrayOps[(some other)_$1(in object $iw) with Object] forSome { type (some other)_$1(in object $iw) <: Enum[_] } 
required: Class[_ <: Enum[_]] => scala.collection.GenTraversableOnce[?B] 
       s.flatMap(_.getEnumConstants) 
       ^
<console>:9: error: type mismatch; 
found : Class[_ <: Enum[_]] => scala.collection.mutable.ArrayOps[(some other)_$1(in object $iw) with Object] forSome { type (some other)_$1(in object $iw) <: Enum[_] } 
required: Class[_ <: Enum[_]] => scala.collection.GenTraversableOnce[B] 
       s.flatMap(_.getEnumConstants) 
         ^
<console>:9: error: Cannot construct a collection of type That with elements of type B based on a collection of type Seq[Class[_ <: Enum[_]]]. 
       s.flatMap(_.getEnumConstants) 
       ^

और, यदि आप अपनाविभाजित करते हैं

scala> s.map(_.getEnumConstants) 
res28: Seq[Array[_$1 with Object] forSome { type _$1 <: Enum[_] }] = List(Array(NANOSECONDS, MICROSECONDS, MILLISECONDS, SECONDS, MINUTES, HOURS, DAYS), Array(RELEASE_0, RELEASE_1, RELEASE_2, RELEASE_3, RELEASE_4, RELEASE_5, RELEASE_6)) 
scala> res28.flatten 
<console>:10: error: No implicit view available from Array[_$1 with Object] forSome { type _$1 <: Enum[_] } => scala.collection.GenTraversableOnce[B]. 
       res28.flatten 
        ^

यही नहीं बल्कि आश्चर्य की बात है जब से तुम लगता है कि है कि यह एक GenTraversableOnce में एक Array चालू करने के लिए आसान होना चाहिए: 10, आप समस्या का एक सरल संस्करण देखने को मिलता है। मैं पल में जानकारी खुदाई करने के लिए समय नहीं है, लेकिन मैं निम्नलिखित बातें काम करने के लिए लग रहे हैं कि बाहर बिंदु होगा:, this gist की वजह से है, जो कुछ बहुत ही अजीब से पता चलता

s.flatMap(_.getEnumConstants.toSeq) 
s.flatMap(_.getEnumConstants.map(_.asInstanceOf[Enum[_]])) 

मैं संकलक बग वोट इस सरल लिपि के लिए आरईपीएल में व्यवहार

val s: Seq[Class[_ <: java.lang.Enum[_]]] = Seq(classOf[java.util.concurrent.TimeUnit], classOf[javax.lang.model.SourceVersion]) 
s.flatMap(_.getEnumConstants.toSeq) 
s.flatMap(_.getEnumConstants.toArray) 
1234 
+0

जैसा कि मेरी दूसरी टिप्पणी में बताया गया है, मैं उम्मीद करता हूं कि 'Predef.genericArrayOps'' ऐरे [टाइमयूनीट] 'से' ArrayOps [TimeUnit] 'तक आवश्यक रूपांतरण प्रदान करता है, जो' GenTraversableOnce [TimeUnit] 'से प्राप्त होता है जैसा कि अपेक्षित है 'फ्लैटमैप' कॉल। –

+0

मैं मानता हूं, सवाल यह बताता है कि यह अंतर्निहित ट्रिगर क्यों नहीं करता है (और शायद यह चाहिए)। – themel

+0

मेरा अनुमान है कि यह मुद्दा आवश्यक अंतर्निहित 'CanBuildFrom [Seq [वर्ग [_ <: java.lang.Enum [_]]], टाइमयूनीट, सेक [टाइमयूनीट]]' से संबंधित है, लेकिन फिलहाल मैं कर सकता हूं यह नहीं पता कि कौन सी स्रोत फ़ाइल में डिफ़ॉल्ट निहित टाइपक्लास बिल्डर्स हैं। मुझे लगता है कि स्कैला-उपयोगकर्ता समूह को पोस्ट करने से कुछ और सूचित उत्तर मांगे जाएंगे। –

2

TimeUnit.getEnumConstants एक जावा सरणी TimeUnit[] देता है, जबकि flatMap उम्मीद एक GenTraversable

आप द्वारा

scala> s.flatMap(_.getEnumConstants.toSeq) 
res4: Seq[Enum[_]] = List(NANOSECONDS, MICROSECONDS, MILLISECONDS, SECONDS, MINUTES, HOURS, DAYS) 
+1

फिर s.flatMap (x => ऐरे (1,2,3)) क्यों काम करता है? – themel

+0

क्षमा करें, यह स्पष्ट नहीं था कि मैं जावा एरे के बारे में बात कर रहा था –

+0

मैं उलझन में हूं, मेरी सरल समझ यह है कि जावा टाइमयूनीट [] स्केल में ऐरे [टाइमयूनीट] जैसा ही है, इसलिए कोई अंतर नहीं होना चाहिए, लेकिन हो सकता है मैं गलत हूं। – themel

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