digitalmars.D.learn - keyword as struct field
- partypooper (12/12) Feb 20 2022 Hello, I'm new to D.
- Basile B. (38/50) Feb 20 2022 I have a library solution based on opDispatch +
- partypooper (9/11) Feb 20 2022 Thank you, didn't know about opDispatch, so you have taught me
- Andrey Zherikov (10/24) Feb 20 2022 Unfortunately I'm not able to help you with mir-ion. But I see
- partypooper (4/8) Feb 20 2022 Yes, but as example in Nim you can surround identifier with
- H. S. Teoh (11/20) Feb 20 2022 The D convention is to append a `_` to the identifier. From
- partypooper (10/12) Feb 20 2022 But this doesn't fix the issue, because mir-ion will throw an
Hello, I'm new to D. Title is self described, is it possible to use keyword as struct field? Maybe it is XYproblem, so here I will describe a little what I need. I'm parsing some json currently with [mir-ion](https://code.dlang.org/packages/mir-ion) (better/simpler suggestions for only json de/serialization?), that has "version" as one of it's keys, so I can't use that as field in my struct. Maybe some annotation in mir library to parse "version" into other struct field name, can't find though (its docs pretty concise).
Feb 20 2022
On Sunday, 20 February 2022 at 11:08:55 UTC, partypooper wrote:Hello, I'm new to D. Title is self described, is it possible to use keyword as struct field? Maybe it is XYproblem, so here I will describe a little what I need. I'm parsing some json currently with [mir-ion](https://code.dlang.org/packages/mir-ion) (better/simpler suggestions for only json de/serialization?), that has "version" as one of it's keys, so I can't use that as field in my struct. Maybe some annotation in mir library to parse "version" into other struct field name, can't find though (its docs pretty concise).I have a library solution based on opDispatch + __traits(getMember): ```d /** * Mixin template allowing to use a field as if its identifier is a D keyword. * Note that this only works with `__traits(getMember)`. * Params: * keywordFieldPairs = An array of keyword and field pairs. */ template FieldOfKeyword(string[2][] keywordFieldPairs) { template opDispatch(string member) { static foreach (pair; keywordFieldPairs) static if (member == pair[0]) mixin("alias opDispatch = ", pair[1], ";" ); } } /// unittest { struct Foo { mixin FieldOfKeyword!([["scope", "scope_"],["class", "class_"]]); string scope_; string class_; } Foo foo; __traits(getMember, foo, "scope") = "The Universe"; assert(__traits(getMember, foo, "scope") == "The Universe"); __traits(getMember, foo, "class") = "Atom"; assert(__traits(getMember, foo, "class") == "Atom"); } ``` never used it TBH.
Feb 20 2022
On Sunday, 20 February 2022 at 14:00:45 UTC, Basile B. wrote:I have a library solution based on opDispatch + __traits(getMember):Thank you, didn't know about opDispatch, so you have taught me something new today. But seems for my current need it can't be apllied (or I still don't understand how to). Anyway I have found in the mir-ion docs examples that it has serdeKeys and serdeKeyOut annotations that can do exactly what I need, bad part it is hard to find them or any documentation about them.
Feb 20 2022
On Sunday, 20 February 2022 at 11:08:55 UTC, partypooper wrote:Hello, I'm new to D. Title is self described, is it possible to use keyword as struct field? Maybe it is XYproblem, so here I will describe a little what I need. I'm parsing some json currently with [mir-ion](https://code.dlang.org/packages/mir-ion) (better/simpler suggestions for only json de/serialization?), that has "version" as one of it's keys, so I can't use that as field in my struct. Maybe some annotation in mir library to parse "version" into other struct field name, can't find though (its docs pretty concise).Unfortunately I'm not able to help you with mir-ion. But I see two questions here:keyword as struct fieldI believe this is the case for the most languages - keyword is not allowed as a variable name.usage of `version` keyword as a variable nameIMO having `version` keyword in D is unexpected language design. No one will complain about keywords that are widely used in the industry (like `struct`, `int` etc). But `version`?! I hit this problem multiple times already and the only solution for me was to use `version_` instead.
Feb 20 2022
On Sunday, 20 February 2022 at 15:33:17 UTC, Andrey Zherikov wrote:On Sunday, 20 February 2022 at 11:08:55 UTC, partypooper wrote:Yes, but as example in Nim you can surround identifier with backticks to omit this issue.keyword as struct fieldI believe this is the case for the most languages - keyword is not allowed as a variable name.
Feb 20 2022
On Sun, Feb 20, 2022 at 04:52:30PM +0000, partypooper via Digitalmars-d-learn wrote:On Sunday, 20 February 2022 at 15:33:17 UTC, Andrey Zherikov wrote:The D convention is to append a `_` to the identifier. From https://dlang.org/dstyle.html: If a name would conflict with a keyword, and it is desirable to use the keyword rather than pick a different name, a single underscore ‘_’ should be appended to it. Names should not be capitalized differently in order to avoid conflicting with keywords. T -- Questions are the beginning of intelligence, but the fear of God is the beginning of wisdom.On Sunday, 20 February 2022 at 11:08:55 UTC, partypooper wrote:Yes, but as example in Nim you can surround identifier with backticks to omit this issue.keyword as struct fieldI believe this is the case for the most languages - keyword is not allowed as a variable name.
Feb 20 2022
On Sunday, 20 February 2022 at 17:02:21 UTC, H. S. Teoh wrote:The D convention is to append a `_` to the identifier. From https://dlang.org/dstyle.html:But this doesn't fix the issue, because mir-ion will throw an exception that there is no version_ field that required and but json string does have "version". I already fixed the issue with mir-ion serdeKeys("version") annotation to struct field. So i'm good. But in Nim backticks are a _little_ bit other thing then just simple convention. Not sure about is's implementation, but at least for situation like that it would work. And it's operator overloading feature uses them too.
Feb 20 2022