I agree here that there is a lot of repetition in the function definitions 
of let's say a generic math package:

types Numbers { type float32,float64,  }

func Abs[T Floats](a T) T {}
func Sin[T Floats](a T) T {}  
func Cos[T Floats](a T) T {}  

etc. (50 orso more?)

It feels a bit redundant and "noisy" to the eye. I think in practice, any 
good generic packages will only use maybe one or two type parameters 
constrainsts (interfaces), and likewise most types (functions) will use 
very similar if not the same type parameters lists.

So I tried to come up with a solution similar to yours by predefining T 
which could then be directly. But as Ian illustrated there's issues that 
are not naturally solved. So I agree that the current proposal is simpler 
(with the downside of being a bit "noisier" wrt such a proposal).

Another solution that comes to mind would be to allow to some sort of 
grouping structure:

generic [T Numbers] {
    func Abs(a T) T {}
    func Sin(a T) T {}  
    func Cos(a T) T {}  
}

This would give all types within the group the same type parameters list.  
The grouping my also be useful for documentation. Obvious downside is the 
extra level of indentation on package level (although it nicely separates 
the generic types from concrete types, so maybe just getting used to). 
Another downside is the introduction of the "generic" keyword. 

-Markus


On Tuesday, March 23, 2021 at 10:16:44 PM UTC+1 atd...@gmail.com wrote:

> Quick question...
>
> Why do we need brackets to define a parametered function, struct etc...?
>
> Why not change Go kinds to accept either a type (would produce a regular, 
> function, structs, etc) or a new type parameter object that would implement 
> the constraints (would produce a generic function definition)
>
> for instance,
>
> type parameter T
>
> // [...]  some way to add constraint to T
>
> func Max(v T) T{...} 
>
> What are the brackets for? Just an idea.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/b802e7fe-4f72-4804-8c23-ea55ac4a5de0n%40googlegroups.com.

Reply via email to