Generics v0

Now argentum classes and interfaces can have parameters:

class HashMap(K, V) { ... }
interface Stream(T) { ... }

Parameters can be used in place of classes in type declarations:

  • in bases,
  • fields,
  • method parameters and results,
  • inside the method bodies.
class HashMap(K, V) {
   +Array(Pair(K, V));                // Base class parameterized with our class parameters
   getAt(key K) ?V { ... }            // Class parameters affect types of method prototypes
   setAt(key K, newVal V) { ... }
   forEach(visitor (K, V)) { ... }    // visitor lambda is typed with class parameters.
   ...
}
interface Stream(T) {
   get() ?T;
}
class Pair(A, B) {
  a = ?A;  // field `a` of type `Optional(A)` initialized with `none`
  b = ?B;  // Field types are also depending on class parameters.
}

In v0 there are three limitations:

  • Parameters can be classes and interfaces (but not value types).
  • Parameters cannot be instantiated and casted to (like in Java).
  • No covariance/contravariance.

Some of these limitations will be reduced in next revisions.

Arrays became generics too:

names = Array(Pair(String, Node));
x = names[0];    // x of type ?Pair(String, Node)
names[0] ? log(_.a); // If array at index 0 contains pair, print its string

connections = WeakArray(Node);

These additions to the Argentum language allow to implement the standard container library and significantly reduce the amount of typecast operators.

Leave a Reply

Your email address will not be published. Required fields are marked *