3 ms·
"This particular Go feature (which arguably came from C) is so open to future compatibility breakage that I wish it had never been included." This is the commu
by jerf 9y ago
"This particular Go feature (which arguably came from C) is so open to future compatibility breakage that I wish it had never been included."
This is the community consensus view. I strongly disagree with it. Unkeyed literals are useful for when you want to get a compile-time error if any fields change, which is common in tuple-like structs. If there is anything wrong with this, it is that in theory you ought to be able to specify at the type declaration whether you must use unkeyed (IMHO a valid use case), must use keyed, or may use either. However this would not generally fit with Go's philosophy.
As a consequence of that theory, I would never use unkeyed literals to create a generated type; too likely that something will change in the generated type with future generators, exactly as I stated.
I suppose this could be layered on in another linter, although since the community and I part ways here even if I wrote "nobody" would use it.
"The second one, about structs no longer being usable as map keys or comparable with ==,"
One of the weirdest things about Go to me is the whiff on making several operations interfaces, like how Python has __del__ and such. == is a particularly weird case; the rules for == are downright dynamic language-ish: https://golang.org/ref/spec#Comparison_operators https://golang.org/ref/spec#Comparison_operators. I imagine they may have considered this "operator overloading" but IMHO that's only a problem on operators that come with a lot of implicit semantics like +, not the "lookup operator".
- perfmode 9y agoUnkeyed literals wont produce a compiler error if fields of the same type and position are replaced. Or if a field is “renamed” to something semantically distinct.
- jerf 9y agoI tend to use lots of little types, so I don't generally end up with {int, int, int} but {Width, Height, Depth} or something. In practice I don't have this problem. YMMV depending on your own practices.
- lobster_johnson 9y agoUnkeyed literals are definitely useful. But the counterargument is that by making keyless field order part of the type, you're creating an implicit protocol, one that can easily be broken, and so it comes with downsides. The odd thing is that Go permits fields to be omitted for the keyed literals, but not for the unkeyed ones. So keyed literals are less strict, as you point out, from the point of view that a caller should know what they're initializing, which is not possible when the evolution of a program mandates that new mandatory fields be added to a struct. I've been bitten by this so many times.† One solution to your desired functionality would to let types declare a constructor that can accept ordered arguments. Then the key order could be part of that signature, and you could support it for all time. I can imagine a bunch of possible syntaxes that could work here. ___ † As an aside, Rust is super strict here: A struct literal must have keys, and all keys are mandatory, although its literal syntax supports splatting, so you can merge in a default empty version: let foo = Foo { something: 42, ..emptyFoo } That gives you the best of both worlds, in my opinion.