proposal: generics: type Inter int8,int16,int32,int64

194 views
Skip to first unread message

haibin wei

unread,
Oct 22, 2020, 12:35:26 PM10/22/20
to golang-nuts

hi:

this is only an idea, hope it's useful.

no `T`, only `type` as `types`.

```go
type Inter int8,int16,int32,int64

func Less(a,b Inter)bool{
  return a<b
}

// and
type UInter uint8,uint16,uint32,uint64
type Float float32,float64
type Number Inter,Uinter,Float

func Less(a,b Number)bool{
  return a<b
}
func Abs(n Number)Number{
  if n<0{return -n}
  return n
}
func Map(s []Number, f func(Number)Number)[]Number{
  r := make([]Number, len(s))
    for i, v := range s {
        r[i] = f(v)
    }
    return r
}

// sorter
type Sorter Number,string
func Sort(list []Sorter){
  // ...
}

// even with interface
type Some interface{String()},string
```

Ian Lance Taylor

unread,
Oct 22, 2020, 3:58:43 PM10/22/20
to haibin wei, golang-nuts
Reply all
Reply to author
Forward
0 new messages