www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - keyword as struct field

reply partypooper <pythonproof gmail.com> writes:
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
next sibling parent reply Basile B. <b2.temp gmx.com> writes:
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
parent partypooper <pythonproof gmail.com> writes:
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
prev sibling parent reply Andrey Zherikov <andrey.zherikov gmail.com> writes:
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 field
I believe this is the case for the most languages - keyword is not allowed as a variable name.
 usage of `version` keyword as a variable name
IMO 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
parent reply partypooper <pythonproof gmail.com> writes:
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:
 keyword as struct field
I believe this is the case for the most languages - keyword is not allowed as a variable name.
Yes, but as example in Nim you can surround identifier with backticks to omit this issue.
Feb 20 2022
parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
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:
 On Sunday, 20 February 2022 at 11:08:55 UTC, partypooper wrote:
 keyword as struct field
I believe this is the case for the most languages - keyword is not allowed as a variable name.
Yes, but as example in Nim you can surround identifier with backticks to omit this issue.
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.
Feb 20 2022
parent partypooper <pythonproof gmail.com> writes:
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