-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Default fields for objects #12378
Default fields for objects #12378
Conversation
Can you restrict the default values even further to also not allow allocated values like "seq" and "string"? Until now I've written a lot of code (for example the json.to macro rewrite), that lives under the assumption that type
MyTypeKind = enum
mtA, mtB, mtC
MyType = object
something: int = -1
case kind: MyTypeKind = mtC
of mtA:
aval: int = 123
of mtB:
bval: float = 456.789
of mtC:
cval: seq[int] = @[1,2,3]
# generated code for macro json.to[MyType]:
proc initFromJson(dst: var MyType, jsonNode: JsonNode; jsonPath: string) =
initFromJson(dst.something, getOrDefault(jsonNode, "something"), jsonPath & ".something")
var kindTmp: MyTypeKind
initFromJson(kindTmp, jsonNode["kind"], jsonPath & ".kind")
dst.kind = kindTmp # with some hack to make this an unsafe assignment that compiles
case dst.kind
of mtA:
initFromJson(dst.aval, getOrDefault(jsonNode, "aval"), jsonPath & ".aval")
of mtB:
initFromJson(dst.bval, getOrDefault(jsonNode, "bval"), jsonPath & ".bval")
of mtC:
initFromJson(dst.cval, getOrDefault(jsonNode, "cval"), jsonPath & ".cval") If you allow |
@krux02 That could be easily done. Question is should we? AFAICT this hack shouldn't be allowed in the first place. I don't oppose your POV but I'm also not convinced we should restrict this feature like that. |
@Clyybber with my PR I was forced to introduce that hack, because currently there is no way in Nim to set the kind field of an object that doesn't also resets everything else in the object. In deserialization I need to set the object fields element by element. |
fbb67f7
to
49b88c1
Compare
I see that you are still active on this. What I am most interested in would be the test cases to see this feature in use. What I would like to see is are tests that cover good error messages for the parts that should not be supported, such as default seq and default string values. |
4cc6b97
to
d1d8ddb
Compare
44c0b60
to
1b4f39e
Compare
19baa01
to
d608f82
Compare
b6b4626
to
44b3f90
Compare
|
I would say
So the reason why we need default values for members are types that are not valid when initialized with zero. And now because values are initialized with something else than zero, we need to have a new function that initializes with zero anyway. In other words you want a |
Furthermore, 0 is special as it allows optimizations by fitting the data in .bss section
|
Yes I think a |
I've reviewed this PR as part of my work on #13808 and there are quite a few things that I would change:
|
This pull request has been automatically marked as stale because it has not had recent activity. If you think it is still a valid PR, please rebase it on the latest devel; otherwise it will be closed. Thank you for your contributions. |
We still want this feature. Right? |
This pull request has been automatically marked as stale because it has not had recent activity. If you think it is still a valid PR, please rebase it on the latest devel; otherwise it will be closed. Thank you for your contributions. |
Good bot. |
Indeed, it's reminding us that we've got a PR for this and hopefully this will prod someone that cares to rebase it :) |
Please take over, @xflywind |
Implements nim-lang/RFCs#126
Only const values are allowed.