निम्नलिखित कोड को परिभाषित करें:क्लासमैनिफेस्ट को ऐरे के साथ क्यों जरूरी है लेकिन सूची नहीं है?
scala> def test[T](iter:java.util.Iterator[Any], func:Any=>T):Array[T] =
| iter.map(i=>func(i)).toArray
<console>:11: error: could not find implicit value for evidence parameter of
type ClassManifest[T]
iter.map(i=>func(i)).toArray
^
दूसरी ओर, का उपयोग कर नीचे दिए गए वैकल्पिक कोड:
import scala.collection.JavaConversions._
val iter:java.util.Iterator[Any] = Array[Any](1, 2, 3).iterator
def func(a:Any):String = a.toString
def test[T:ClassManifest](iter:java.util.Iterator[Any], func:Any=>T):Array[T] =
iter.map(i=>func(i)).toArray
def testFunc = test(iter, func)
यहाँ, मैं नहीं तो मैं त्रुटि मिलती है ClassManifest
उपयोग करने के लिए इसे सही ढंग से संकलित करने के लिए के लिए की जरूरत है, List
इसकी आवश्यकता नहीं है और ठीक से संकलित करता है।
import scala.collection.JavaConversions._
val iter:java.util.Iterator[Any] = Array[Any](1, 2, 3).iterator
def func(a:Any):String = a.toString
def test1[T](iter:java.util.Iterator[Any], func:Any=>T):List[T] =
iter.map(i=>func(i)).toList
def testFunc1 = test1(iter, func).toArray
ध्यान दें कि testFunc
और testFunc1
के अंतिम उत्पादन समान हैं।
List
संस्करण को ClassManifest
की आवश्यकता नहीं है?
आपका और एंजेल का जवाब एक साथ मिलकर सही जवाब बनाता है :) – Jus12