by everybodyknows · 4mo ago · 0 comments
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]"?
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.
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.