[generics] Use decorators ($, or @, or #) to specify that the type is a generic type.

155 views
Skip to first unread message

Andrey T.

unread,
Jun 25, 2020, 12:17:24 AM6/25/20
to golang-nuts

The idea: Use decorators ($, or @, or #) to specify that the type is a generic type.

(Syntax is vaguely follows to * "it is a pointer", so $T becomes a "it is a generic type")

1. Readability - Immediately visible which type is a generic type.

2. Readability - Less braces. Compare

   func (type T) RandomItem(a ...T) T { ... }

   becomes

   func RandomItem(a ...$T) $T { ... }

3. Ability to use decorated interface name as spec for type constrains

   func (type T Comparable) Max(a... T) (result T) {...}

   might become

   func Max(a... $Comparable) (result $Comparable) {...}

4. It does not prevent usage of type keyword if wanted/needed.

Thanks,
  Andrey

PS.
it is a distill of my previous post https://groups.google.com/forum/#!topic/golang-nuts/Rp3yUUy2nS8, which asked a different question about "How often do we have to specify type during invocation", and this idea got a bit lost in it.

haskell...@yahoo.de

unread,
Jun 25, 2020, 12:55:10 AM6/25/20
to golang-nuts
On Thursday, 25 June 2020 06:17:24 UTC+2, Andrey T. wrote:
 
3. Ability to use decorated interface name as spec for type constrains 

   func (type T Comparable) Max(a... T) (result T) {...}

   might become

   func Max(a... $Comparable) (result $Comparable) {...}

I don't see how this is equivalent. The point of the T constraint is that both T are the same Comparable type and not just any two Comparables.

Andrey T.

unread,
Jun 25, 2020, 2:17:35 AM6/25/20
to golang-nuts
Good point. Without some additional rules to "freeze" it once seen in a declaration, that would become "apples" to "oranges" very fast.

Well, I guess scratch point 3 off the list then :)
Reply all
Reply to author
Forward
0 new messages