[generics] generic types join through pipes

160 views
Skip to first unread message

Reindert Vetter

unread,
Nov 20, 2020, 5:58:38 PM11/20/20
to golan...@googlegroups.com
Good evening,

First of all, thank you for everything you do with Go!

I've had an idea for generic for Go for some time. If it makes no sense, please ignore my email.

The idea is to join the different types through a pipe. This means you do not have to introduce a 'type' in the interface. It is also much easier to convert ordinary types to a generic type.

Some examples:

type typeFloat float64
type typeInt int
type stringAble typeFloat|typeInt

func ConvertToString(input stringAble) string {
   //
}

type stringAble float64|int

func ConvertToString[type T stringAble](input []T) string {
   //
}

With interfaces:
type stringAble typeFloat|typeInt|error|stringer

func ConvertToString(input stringAble) string {
//
}

They may not be generics, but you could also allow the following:


func
ConvertToString(input float64|int) string {
   //
}

type typeFloat float64
type typeInt int

func ConvertToString(input typeFloat|typeInt) string {
   //
}

What do you think of this idea?

Thanks in advance!

Reindert Vetter

Ian Lance Taylor

unread,
Nov 20, 2020, 10:39:52 PM11/20/20
to Reindert Vetter, golan...@googlegroups.com
This has some similarities to sum types: https://golang.org/issue/19412.

I'm not sure how to write a genreric Min function with this approach.

Thanks for the note.

Ian
Reply all
Reply to author
Forward
0 new messages