@uncheckedVariance
का उपयोग स्कैला की घोषणा साइट भिन्नता एनोटेशन और जावा के इनवेरिएंट जेनेरिक के बीच के अंतर को पुल करने के लिए किया जा सकता है।स्कैला में @uncheckedVariance की आवश्यकता कब है, और इसका उपयोग जेनेरिक ट्रेवरेबल टेम्पलेट में क्यों किया जाता है?
scala> import java.util.Comparator
import java.util.Comparator
scala> trait Foo[T] extends Comparator[T]
defined trait Foo
scala> trait Foo[-T] extends Comparator[T]
<console>:5: error: contravariant type T occurs in invariant position in type [-T]java.lang.Object with java.util.Comparator[T] of trait Foo
trait Foo[-T] extends Comparator[T]
^
scala> import annotation.unchecked._
import annotation.unchecked._
scala> trait Foo[-T] extends Comparator[T @uncheckedVariance]
defined trait Foo
यह कहना है कि java.util.Comparator स्वाभाविक रूप से विपरीत संस्करण है, उस प्रकार पैरामीटर T
मानकों में और एक वापसी प्रकार में कभी नहीं दिखाई देता है।
यह सवाल पूछता है: इसका उपयोग स्कैला संग्रह पुस्तकालय में क्यों किया जाता है जो जावा इंटरफेस से विस्तारित नहीं होता है?
trait GenericTraversableTemplate[+A, +CC[X] <: Traversable[X]] extends HasNewBuilder[A, CC[A] @uncheckedVariance]
यह व्याख्या के लिए मान्य का उपयोग करता है क्या हैं?
महान प्रश्न! –