3 ms·
Well, that doesn't really work, as "StaticOptional[T]" can't be used as the type of a variable. It can only be used to create another generic function. For exa
by tsimionescu 5y ago
Well, that doesn't really work, as "StaticOptional[T]" can't be used as the type of a variable. It can only be used to create another generic function.
For example, this doesn't work [0]:
//compilation error: interface contains type constraints
func TryParse123(s string) StaticOptional[int] {
if s == "123" {
return StaticSome[int]{123}
}
return StaticNone{}
}
[0] https://go.dev/play/p/1CnieCLqESC https://go.dev/play/p/1CnieCLqESC
- Kostarrr 5y agoMy bad. I was not aware on incredibly limited go generics are. I tried to make it work despite all restrictions but gave up here https://go.dev/play/p/XVTuJ--ZLgS https://go.dev/play/p/XVTuJ--ZLgS
- worg 5y agoI'm not well versed in golang generics (at $WORK we use go, but not generics yet) but if you want type switches you'll need interface{} to do that https://go.dev/play/p/1Tn2O_7vCTQ https://go.dev/play/p/1Tn2O_7vCTQ Which of course defeats the point of using generics _edit: clarify that my attempt is a bad practice_