4 ms·
I wish there was a way to have generics without: * letting people being able to abuse them. C++ cough cough. * making it clear to people reading the code what
by baby 7y ago
I wish there was a way to have generics without:
* letting people being able to abuse them. C++ cough cough.
* making it clear to people reading the code what is happening.
I’ve noticed a few things:
* associated types in Rust are much clearer than generics
* one letter generics are “overly generic” and do not describe enough.
* the declaration of the type really adds verbosity
So what can a language do?
1. Maybe change the syntax to this one:
func eat(food []generic.Type)
Or
func eat(food []g::type)
Or something that doesn’t involve adding more <> or ()
2. Forbid one letter generic or force a description of the generic or even better: force listing all the types that are currently using this generic NEXT to the generic!
g.type -> {egg, bacon}
func eat(food []g.type)
But then you can’t use a generic from another package... but maybe it’s a good limitation?
Or force a generic to have a description, which is what golang is doing with interfaces. The problem is that we can’t combine difference interfaces like in Rust/ocaml
- protomolecule 7y ago"or even better: force listing all the types that are currently using this generic" This is a bad idea. Imagine a generic hash map, we surely want it to work with any value type.
- baby 7y agoTrue. I’m trying to find a solution to the biggest problem of generics which is imo that you can’t understand how it’s being used when reading one. It’s fine if the function doesn’t do much. But if it’s nesting generics, or doing anything complex, then not using a union of interfaces make it really hard to understand code. I’d like if Golang would add unions of interfaces. But generics? I’m not sure if it’s a good idea. Or maybe if you import a package that have some generic functions, you would need to add a declaration that list what the types using these generics are. Import “stuffWithGeneric{ thing(K, V) -> (egg, bacon), thing(K, V) -> (int, string), }”
- protomolecule 7y agoImagine you have a non-generic function, for example, `parseJsonData(string)`, how would you understand how it is being used? It can be called with all sorts of input. Is the problem you are describing different?
- baby 7y agoThat’s true. Usually the name of the argument indicates how it is used. Without it you are lost. I feel like what really tricks me is this kind of code: parseJsonData(t T) Now neither the type nor the argument name help you understanding the purpose of the function.
- protomolecule 7y agoIn this case wouldn't the name of a contract be enough? Or, perhaps, not enough, but informative to the same degree?
- spion 7y agoThe one-letter "naming convention" used for generic types by most developers is very unfortunate.
- protomolecule 7y agoWhy? There are usually only one or two of them, it's like using `i` for loops instead of `indexOfElement`.
- spion 7y agoType parameters are more like function arguments, not loop variables. There is often one or two arguments too but they are not as local (contained) as loop variables.
- thomasz 7y agoThis is a problem with template metaprogramming, not with generics. Code using generics isn’t less obvious than code using only specialized types. On the contrary: You work with strong typing instead of using interface {} everywhere.
- mehrdadn 7y ago> without... letting people being able to abuse them. C++ cough cough. Curious what you consider "abuse", given the standard library itself uses generics in all sorts of ways?
- thomasz 7y agoIn other languages, you can easily define constraints on generic types. It’s not rocket science, but actively ignoring any advancement since the 70s means that you have to reinvent the wheel constantly.
- baby 7y agoGolang could do this by implementing unions on interfaces.
- convolvatron 7y agoI certainly agree with your later example (cars+planes)...but how do union types help with subtyping?
- baby 7y agohow do generics help with subtyping?
- bno1 7y ago>1. Maybe change the syntax to this one: >func eat(food []generic.Type) Or >func eat(food []g::type) Or something that doesn’t involve adding more <> or () I don't understand how this would work with multiple generic parameters. Say I have func merge(a []g::type, b []g::type) []g::type How do you specify that a, b and the returned value must have the same type and implement the comparison operators? Also, why are people trying so hard to stay away from < and >?
- baby 7y ago“type” here is whatever you want it to be func merge(a []g::thing, b []g::thing) []g::otherThing The <> syntax is well accepted, that’s the only thing it has. What’s annoying is that it adds to the function signature when it’s not necessary.
- mamcx 7y agoI also dislike the use of fun<T>(a:T b:T)->T because it make look weird. How about: fun(a:$T b:$T) -> $T //to look like templating fun(a::T b::T) -> ::T //to look like "a type but depper" fun(a:_T b:_T) -> _T //to look like "a type hole" mmm... I like more the third :)