Improve usage of the "variant" idiom #966
Tetralux
started this conversation in
Ideas/Requests
Replies: 2 comments
-
|
My only concern for this feature is that it would only make sense for |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
One could argue though that |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
The "variant" idiom used in
encoding/jsonis a neat idiom that makes the code simple, but also quite powerful. It's also used in type information.However, the usage of it is more confusing and awkward than it really needs to be.
The extra
valuein there is a sideproduct of how there's common fields in thejson.Value.This is quite a neat way of structuring the library, and also allows the variants to just be distinct types; it allows a
json.Stringto be easily converted to and fromstring:json.String("hi")/string(my_json_str)However, what you really want is a
using-like thing:obj := value.(json.Object);.Maybe with
using value: union {injson.Value.This could perhaps then allow you to
obj := value.(json.Value);, while still allowingvalue.posandvalue.endto work.Beta Was this translation helpful? Give feedback.
All reactions