3 ms·
I agree; there are a lot of reasons to have generics in a language, performance one of them as demonstrated; but anyway it doesn't make sense if in a strictly,
by Rochus 4y ago
I agree; there are a lot of reasons to have generics in a language, performance one of them as demonstrated; but anyway it doesn't make sense if in a strictly, statically typed language we are forced to do dynamic typing to make data structures and algorithms reusable; however, i don't understand why everyone always goes for generic types; generic modules can do that too and are much easier to understand and implement.
- ayende 4y agoCan you explain what you mean by generics modules?
- Rochus 4y agoHere is an example: https://oberon-lang.github.io/ https://oberon-lang.github.io/, see section Generic Programming; note how the modules have type parameters and how generic module is instantiated by the import declaration in the example of section Object Oriented Programming. Here is the specification: https://github.com/oberon-lang/specification/blob/master/The_Programming_Language_Oberon%2B.adoc#generics https://github.com/oberon-lang/specification/blob/master/The... Here is a discussion why it is designed like this: https://oberon-lang.github.io/2021/07/17/considering-generics.html https://oberon-lang.github.io/2021/07/17/considering-generic...
- ayende 4y agoThat reminds me very strongly how Zig does it
- Rochus 4y agoI'm not familiar with Zig, but a quick look at https://ziglang.org/documentation/master/#Generic-Data-Structures https://ziglang.org/documentation/master/#Generic-Data-Struc... makes me doubt that Zig does genericity on module level; looks rather like parametric polymorphism.
- mncharity 4y agoNot GP, and I'm just surfing unfamiliar docs, but it looks like, yes, there's general parameter polymorphism,[1] but also "modules" presented as structs,[2] and the polymorphism of struct constructors can have nontrivial impacts[3](eg Map(KeyType,void) loses value setters). Fwiw. [1] https://ziglang.org/documentation/master/#comptime https://ziglang.org/documentation/master/#comptime "Compile-time parameters is how Zig implements generics. It is compile-time duck typing." [2] https://ziglang.org/documentation/master/#import https://ziglang.org/documentation/master/#import [3] https://ziglang.org/documentation/master/#void https://ziglang.org/documentation/master/#void "By using void as the type of the value, the hash map entry type has no value field, and thus the hash map takes up less space. Further, all the code that deals with storing and loading the value is deleted, as seen above."
- Rochus 4y agointeresting, thanks
- mncharity 4y agoThanks for this. By coincidence, I was sketching out a polymorphism kludge this evening, and this was food for thought.
- nine_k 4y agoI'd say that no matter where, but you have to add generic parameters to algorithms to make them reusable across domains. Doing so at type / function level allows to introduce them gradually and with low ceremony, compared to parametrized modules (likely you mean OCaml here?). Gradual approaches seem to work best in real world, hence the prevalence of generic functions and parametrized types.
- Rochus 4y ago> likely you mean OCaml here No, I was thinking of Ada or Oberon+. Here is a discussion: https://oberon-lang.github.io/2021/07/17/considering-generics.html https://oberon-lang.github.io/2021/07/17/considering-generic...