On Tue, 7 Jul 2020 at 21:05, Tobias Gustafsson < tobias.l.gustafs...@gmail.com> wrote:
> Den tisdag 7 juli 2020 kl. 16:45:21 UTC+2 skrev rog: >> >> On Tue, 7 Jul 2020 at 10:36, Tobias Gustafsson <tobias.l...@gmail.com> >> wrote: >> >>> Hi all, >>> >>> Thanks for the response on this subject! >>> >>> Yes, a kind of type switch over generic types is probably what I >>> envision. There is a potentially nice symmetry with the current runtime >>> type switches and type annotations that caught my eye (being aware of the >>> fact that finding false patterns and symmetries may lead you astray...). >>> >>> If we do manage to set the semantics for a type switch over a generic >>> type I'd expect that to also spill over to type assertions over generic >>> types. Perhaps it would also be easier to start with at type assertion to >>> explore the problem space a bit more before heading directly for the type >>> switch. >>> >> >> That's not technically needed AFAICS, given that you can already convert >> to interface{} and then type switch, but it's potentially nice as syntax >> sugar. >> > > Not sure I completely understand what you mean by this, what I meant is > basically that I would find it good to be able to do things like this: > > var x T // Generic type T > s := x.(string) // s is a string or compile time error > > s, ok := x.(string) // s is the string x represents if T is of type string, > // otherwise a zero value string. > // Any code relying on ok to be true can be eliminated > // during compilation if type assertion fails and vice > // versa. > By "convert to interface{}", I meant using "interface{}(x)" to convert to a type-switchable value. For example: https://go2goplay.golang.org/p/txdvJauonEu It might be nice to be able to do that without the conversion to interface{}, but it's not technically *necessary*. cheers, rog. -- 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/CAJhgachiRNc1X0oZW0BATuL7Y1uoWMmmKm5bzhiVpCKW77PgYQ%40mail.gmail.com.