|
|
Log in / Subscribe / Register

What's coming in Go 1.18

What's coming in Go 1.18

Posted Feb 9, 2022 8:45 UTC (Wed) by taladar (subscriber, #68407)
Parent article: What's coming in Go 1.18

> func min(x float32, y float32) float32 {
> if x < y {
> return x
> }
> return y
> }

> type ordered interface {
> float32 | float64 | int
> }
> func min[T ordered](x T, y T) T {
> if x < y {
> return x
> }
> return y
> }

That looks like it is significantly less expressive than proper generics with open traits/type classes/interfaces/...

Can you really only specify a closed, one-time list without being able to add new types to it later?


to post comments

What's coming in Go 1.18

Posted Feb 9, 2022 11:30 UTC (Wed) by jmaa (guest, #128856) [Link]

I _think_ you can define method interfaces, which should allow full generics, but the example I found of this is a year old. [1] The linked generics tutorial doesn't mention them, so who knows.

[1]: https://www.freecodecamp.org/news/generics-in-golang/

What's coming in Go 1.18

Posted Feb 9, 2022 13:19 UTC (Wed) by tsavola (subscriber, #37605) [Link]

constraints.Ordered was supposed to be shipped in the standard library (I guess it will be added in a later release). Go doesn't support operator overloading, so only primitive types (and ~primitive types) can have the < operator.

For generic code invoking methods, you can define interfaces with methods - you don't have to list concrete types.


Copyright © 2026, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds