13
votes
What are some syntax options for describing generic ("templated") types?
Square Brackets
Python's optional typing module uses square brackets for this, such as list[int]. The PEP that added this syntax has a lengthy analysis of why they ...
13
votes
What are some syntax options for describing generic ("templated") types?
First-class types
In this case, we introduce a type for types: U, then we have List : U -> U, so naturally we would use the ...
9
votes
Accepted
What strategies did Go use to circumvent the lack of generics?
There are two questions lurking here: what do people do in the general case for their own types, and what did they do for basic collections? The choice in (original) Go was that these did not need to ...
9
votes
Accepted
Pros and cons of overloading vs type classes or similar polymorphism
These do not need to be exclusive options: Scala has both typeclasses and overloads. Here are two ways to implement println:
...
9
votes
What strategies did Go use to circumvent the lack of generics?
AFAIK, most Gophers tend to replace generics with duplicating classes, merely changing the contained type and the name to reflect it. A similar approach is used in C, which has macros and therefore ...
8
votes
What are some syntax options for describing generic ("templated") types?
Just, parentheses
This is what D does. For example, the following class in C#:
public class Bar<T>
{
public T member;
}
looks like this in D:
...
7
votes
What are some syntax options for describing generic ("templated") types?
A pairing that you're willing to give up on using for any other purpose
Angle brackets have a strong history in this role, but < and ...
7
votes
What are some syntax options for describing generic ("templated") types?
Spaces
This would look like List float, Map String int, or Option Box char.
Pros:
Looks ...
7
votes
What are some syntax options for describing generic ("templated") types?
Specialised syntax
Like Haskell, you could use specialised syntax for specific kinds of types. For example: (A -> B) for a function that receives an argument of ...
6
votes
What strategies did Go use to circumvent the lack of generics?
Go always had generics!
Except they were limited to built-in types and had special syntax support.
Generic arrays are spelt [n]a, slices ...
5
votes
How to choose between monomorphisation and type erasure for generics?
Monomorphization is less expressive than type erasure. The main limitation is that in order to monomorphize, the compiler must know the set of potential types. In some situations, this can be ...
5
votes
How to choose between monomorphisation and type erasure for generics?
Why not a bit of both?
This is how Swift does it, AFAIK. for private/internal functions where it knows exactly how it’ll be called, it monomorphises. For public functions in a library and ...
5
votes
What strategies did Go use to circumvent the lack of generics?
Using interface{} and type assertions
Go allows for dynamic dispatch using interfaces, which contain the methods needed for a ...
4
votes
Prior art on precedence rules on template instantiation for inner entity clashes
In C#, it's not an error. If E is Exception, then the first catch gets executed:
...
4
votes
How to choose between monomorphisation and type erasure for generics?
One issue with type erasure is that it requires all possible types that the function might be called with, to have some fixed size in memory; in contrast, a monomorphised function can be specialised ...
4
votes
Accepted
How to choose between monomorphisation and type erasure for generics?
Here are a few differences between monomorphisation and type erasure:
Performance: Monomorphisation generally leads to better runtime performance because each specialised version of the function is ...
4
votes
What are some syntax options for describing generic ("templated") types?
Lisp-style
Another option which would work with any choice of brackets is a more lisp-style syntax: (Map String Int), ...
3
votes
What are some syntax options for describing generic ("templated") types?
For classes, just parentheses
D language does this, as already mentioned. Direct example from your question:
...
3
votes
What are some syntax options for describing generic ("templated") types?
Ada's approach to generic programming consists in having a generic block before the procedure being declared:
...
3
votes
What strategies did Go use to circumvent the lack of generics?
Disclaimer: I don't have much experience in Go, and this answer is written from the limited experience I have. If I've got anything wrong in this answer, please leave a comment and I'll try to correct ...
3
votes
Does the C# type of generics need compiling to multiple instances of target code?
When T is a ValueType (i.e a primitive or a struct), the generic will be monomorphized and there will be one implementation for ...
2
votes
Accepted
Does the C# type of generics need compiling to multiple instances of target code?
I'm not sure what the official .NET implementation does these days, but from what I remember from working on an ahead-of-time compiler for C# a while back, the main constraints that limit sharing code ...
2
votes
How could a language implement generic traits?
Primary associated types
Swift added this in version 5.7, and it was a huge boon to the usability of protocols. With this syntax, you can pick and choose which associated types can act like generics:
<...
1
vote
How to choose between monomorphisation and type erasure for generics?
The language specification must explain if what is called "generics" here means forall or lambda polymorphism. Both have different semantics, for functions and types:
...
1
vote
How to choose between monomorphisation and type erasure for generics?
Regarding usability (i.e. programming),
C#, and Java (probably), generics feel from the inside as if programming to an interface. Basically the interface constraining the type parameter, or Object if ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
generics × 8monomorphization × 2
type-erasure × 2
language-comparison × 1
syntax × 1
semantics × 1
types × 1
implementation × 1
ambiguity × 1
design-choice-comparison × 1
static-typing × 1
polymorphism × 1
type-checker × 1
c# × 1
traits × 1
language-ergonomics × 1
overloading × 1