Skip to main content
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 ...
pppery's user avatar
  • 939
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 ...
ice1000's user avatar
  • 2,694
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 ...
Michael Homer's user avatar
  • 13.6k
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: ...
RubenVerg's user avatar
  • 1,661
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 ...
kouta-kun's user avatar
  • 1,668
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: ...
Bbrk24's user avatar
  • 9,407
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 ...
Michael Homer's user avatar
  • 13.6k
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 ...
rydwolf's user avatar
  • 4,582
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 ...
Jasmijn's user avatar
  • 1,077
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 ...
Longinus's user avatar
  • 1,883
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 ...
Rufflewind's user avatar
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 ...
Bbrk24's user avatar
  • 9,407
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 ...
bigyihsuan's user avatar
  • 1,851
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: ...
Eldritch Conundrum's user avatar
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 ...
kaya3's user avatar
  • 20.8k
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 ...
The Thonnu's user avatar
  • 1,566
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), ...
rydwolf's user avatar
  • 4,582
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: ...
André L F S Bacci's user avatar
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: ...
coredump's user avatar
  • 943
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 ...
The Thonnu's user avatar
  • 1,566
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 ...
John's user avatar
  • 101
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 ...
Dave Bartolomeo's user avatar
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: <...
Bbrk24's user avatar
  • 9,407
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: ...
feldentm's user avatar
  • 2,192
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 ...
Pablo H's user avatar
  • 309

Only top scored, non community-wiki answers of a minimum length are eligible