3 ms·
> Since the addition of generics in Go 1.18 and iterators in Go 1.23, it has become possible for library-defined types to achieve comparable ergonomics to built
by everybodyknows 2mo ago
> Since the addition of generics in Go 1.18 and iterators in Go 1.23, it has become possible for library-defined types to achieve comparable ergonomics to built-in types ...
For those who've been following Golang more diligently than myself: Is there a way now to define a slice-based type that enforces strict typing of its index, and yet preserves the compactness of the standard syntax "s[i]"?
- bheadmaster 2mo agoWhat do you mean by "strict typing of its index"?
- dwattttt 2mo agoI expect it's referring to the newtype pattern; you don't want to accidentally add inches to cm, so if you have to handle both of them a lot, you make a single field struct Inches and one for Cm. For containers, you can do this to make it a type error to index the container with anything other than the newtype you made. In some languages you can replace/implement the indexing operation a[i] with your new type, so the code looks the same as usual, but gets the benefit of preventing the wrong 'integer' from being used.
- kubanczyk 2mo ago> Is there a way now to define a slice-based type that enforces strict typing of its index Nope, no way to do that. I wonder about ergonomics. I can imagine "academic" usage, but I shudder at the perspective of the impact on a typical CRUD app.