Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Most people seem to prefer declaration-site variance, because it makes it easier for <strong>users</strong> of the library (while making it a bit harder for the library developer, although I would argue that the library developer has to think about variance regardless of where the variance is actually written.)</p> <p>But keep in mind, that neither Java nor C# are examples of good language design.</p> <p>While <strong>Java</strong> got variance right and working independently of the JVM because of compatible VM improvements in Java 5 and type-erasure, the use-site variance makes usage a bit cumbersome and the particular implementation of type-erasure has drawn well-deserved criticism.</p> <p><strong>C#</strong>'s model of declaration-site variance takes the burden away from the user of the library, but during their introduction of reified generics they basically built the variance rules into the their VM. Even today they can't fully support co-/contravariance because of this mistake (and the non backward-compatible introduction of the reified collection classes has split the programmers into two camps).</p> <p>This poses a difficult restriction on all languages targeting the CLR and is one reason why alternative programming languages are much more lively on the JVM although it seems that the CLR has "much nicer features".</p> <p>Let's look at <strong>Scala</strong>: Scala is an fully object-oriented, functional hybrid running on the JVM. They use type erasure like Java, but both the implementation of Generics and the (declaration-site) variance are easier to understand, more straightforward and powerful than Java's (or C#'s), because the VM doesn't impose rules on how variance has to work. The Scala compiler checks the variance notations and can reject unsound source code <em>at compile time</em> instead of throwing exceptions at runtime, while the resulting .class files can seamlessly be used from Java.</p> <p>One disadvantage of declaration site variance is that it seems to make type inference harder in some cases.</p> <p>At the same time Scala can use primitive types without boxing them in collections like in C# by using the <code>@specialized</code> annotation which tells the Scala compiler to generate one or multiple additional implementations of a class or method specialized to the requested primitive type.</p> <p>Scala can also "almost" reify generics by using Manifests which allows them to retrieve the generic types at runtime like in C#.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload