www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Concerns about using struct initializer in UDA?

reply Andre Pany <andre s-e-a-p.de> writes:
Hi,

I know there are concerns about struct initialization in method 
calls but what is about struct initializer in UDA?

Scenario: I want to set several UDA values. At the moment I have 
to create for each value a structure with exactly 1 field. But it 
would be quite nice if I could use struct initialization to group 
these values:

struct Field
{
	string location;
	string locationName;
}

struct Foo
{
	 A = {locationName: "B"} int c; // <------------------
}

void main() {}

Of course the syntax is questionable, it is just a proposal.
What do you think?

Kind regards
André
May 11 2017
next sibling parent reply Stefan Koch <uplink.coder googlemail.com> writes:
On Thursday, 11 May 2017 at 10:49:58 UTC, Andre Pany wrote:
 Hi,

 I know there are concerns about struct initialization in method 
 calls but what is about struct initializer in UDA?

 Scenario: I want to set several UDA values. At the moment I 
 have to create for each value a structure with exactly 1 field. 
 But it would be quite nice if I could use struct initialization 
 to group these values:

 struct Field
 {
 	string location;
 	string locationName;
 }

 struct Foo
 {
 	 A = {locationName: "B"} int c; // <------------------
 }

 void main() {}

 Of course the syntax is questionable, it is just a proposal.
 What do you think?

 Kind regards
 André
We have that syntax already.
May 11 2017
parent reply Andre Pany <andre s-e-a-p.de> writes:
On Thursday, 11 May 2017 at 10:51:09 UTC, Stefan Koch wrote:
 On Thursday, 11 May 2017 at 10:49:58 UTC, Andre Pany wrote:
 Hi,

 I know there are concerns about struct initialization in 
 method calls but what is about struct initializer in UDA?

 Scenario: I want to set several UDA values. At the moment I 
 have to create for each value a structure with exactly 1 
 field. But it would be quite nice if I could use struct 
 initialization to group these values:

 struct Field
 {
 	string location;
 	string locationName;
 }

 struct Foo
 {
 	 A = {locationName: "B"} int c; // <------------------
 }

 void main() {}

 Of course the syntax is questionable, it is just a proposal.
 What do you think?

 Kind regards
 André
We have that syntax already.
I do not understand. Should the syntax I have written already work as I expect or do you mean my proposal is not possible as the syntax is ambiguous? Kind regards André
May 11 2017
parent reply Stefan Koch <uplink.coder googlemail.com> writes:
On Thursday, 11 May 2017 at 11:36:17 UTC, Andre Pany wrote:
 On Thursday, 11 May 2017 at 10:51:09 UTC, Stefan Koch wrote:
 On Thursday, 11 May 2017 at 10:49:58 UTC, Andre Pany wrote:
 [...]
We have that syntax already.
I do not understand. Should the syntax I have written already work as I expect or do you mean my proposal is not possible as the syntax is ambiguous? Kind regards André
I thought it should have worked already. My apologies the struct literal initialization syntax is unsupported because of the parser implementation. I don't know if you would introduce new ambiguities; I suspect that you wouldn't.
May 11 2017
parent reply Andre Pany <andre s-e-a-p.de> writes:
On Thursday, 11 May 2017 at 11:57:01 UTC, Stefan Koch wrote:
 On Thursday, 11 May 2017 at 11:36:17 UTC, Andre Pany wrote:
 On Thursday, 11 May 2017 at 10:51:09 UTC, Stefan Koch wrote:
 On Thursday, 11 May 2017 at 10:49:58 UTC, Andre Pany wrote:
 [...]
We have that syntax already.
I do not understand. Should the syntax I have written already work as I expect or do you mean my proposal is not possible as the syntax is ambiguous? Kind regards André
I thought it should have worked already. My apologies the struct literal initialization syntax is unsupported because of the parser implementation. I don't know if you would introduce new ambiguities; I suspect that you wouldn't.
Does it make sense to open a DIP? It would make UDA quite nice to use if you can group several attributes into a struct. Or someone know whether there are already known issues why it isn't possible? Kind regards André
May 12 2017
parent reply Seb <seb wilzba.ch> writes:
On Friday, 12 May 2017 at 10:13:02 UTC, Andre Pany wrote:
 Does it make sense to open a DIP? It would make UDA quite nice 
 to use if you can group several attributes into a struct.

 Or someone know whether there are already known issues why it 
 isn't possible?
The entire point of a DIP is to do this research for everyone and summarize it an easy-to-access and easy-to-find fashion ;-) FWIW there is an abandoned DIP for in-place struct initialization (https://github.com/dlang/DIPs/pull/22) - it might make sense to invest the effort to revive it from the dead...
May 12 2017
parent Andre Pany <andre s-e-a-p.de> writes:
On Friday, 12 May 2017 at 10:25:07 UTC, Seb wrote:
 On Friday, 12 May 2017 at 10:13:02 UTC, Andre Pany wrote:
 Does it make sense to open a DIP? It would make UDA quite nice 
 to use if you can group several attributes into a struct.

 Or someone know whether there are already known issues why it 
 isn't possible?
The entire point of a DIP is to do this research for everyone and summarize it an easy-to-access and easy-to-find fashion ;-) FWIW there is an abandoned DIP for in-place struct initialization (https://github.com/dlang/DIPs/pull/22) - it might make sense to invest the effort to revive it from the dead...
If I find time I will create a new dip. The dip you mentioned is about a different usage scenario. My proposal is only about sructures used as UDA. As far as I remember the old dip was not possible due to comma syntax which is now in deprecation phase. Kund regards André
May 12 2017
prev sibling parent reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Thursday, 11 May 2017 at 10:49:58 UTC, Andre Pany wrote:
 Hi,

 I know there are concerns about struct initialization in method 
 calls but what is about struct initializer in UDA?

 Scenario: I want to set several UDA values. At the moment I 
 have to create for each value a structure with exactly 1 field. 
 But it would be quite nice if I could use struct initialization 
 to group these values:

 struct Field
 {
 	string location;
 	string locationName;
 }

 struct Foo
 {
 	 A = {locationName: "B"} int c; // <------------------
 }

 void main() {}

 Of course the syntax is questionable, it is just a proposal.
 What do you think?

 Kind regards
 André
I'm not sure if it isi what you're looking for but you can do struct Field { string location; string locationName; } struct Foo { Field("","B") int c; } We use this for our compiler recognised attributes in LDC, see llvmAttr for an example (https://github.com/ldc-developers/druntime/blob/ldc/src/ldc/attributes.d#L108)
May 12 2017
parent reply Andre Pany <andre s-e-a-p.de> writes:
On Friday, 12 May 2017 at 10:23:34 UTC, Nicholas Wilson wrote:
 On Thursday, 11 May 2017 at 10:49:58 UTC, Andre Pany wrote:
 Hi,

 I know there are concerns about struct initialization in 
 method calls but what is about struct initializer in UDA?

 Scenario: I want to set several UDA values. At the moment I 
 have to create for each value a structure with exactly 1 
 field. But it would be quite nice if I could use struct 
 initialization to group these values:

 struct Field
 {
 	string location;
 	string locationName;
 }

 struct Foo
 {
 	 A = {locationName: "B"} int c; // <------------------
 }

 void main() {}

 Of course the syntax is questionable, it is just a proposal.
 What do you think?

 Kind regards
 André
I'm not sure if it isi what you're looking for but you can do struct Field { string location; string locationName; } struct Foo { Field("","B") int c; } We use this for our compiler recognised attributes in LDC, see llvmAttr for an example (https://github.com/ldc-developers/druntime/blob/ldc/src/ldc/attributes.d#L108)
I think there are cases where this syntax is more readable and there are cases where struct initialization is more readable. In the example you gave, I do not know what is the second field about and having to mention the first field feels like a burden. As developer I want the choice to decide between these 2 syntax options. Kind regards André
May 12 2017
parent reply ag0aep6g <anonymous example.com> writes:
On 05/12/2017 09:01 PM, Andre Pany wrote:
 On Friday, 12 May 2017 at 10:23:34 UTC, Nicholas Wilson wrote:
[...]
 I'm not sure if it isi what you're looking for but you can do

  struct Field
  {
      string location;
      string locationName;
  }

  struct Foo
  {
       Field("","B") int c;
  }
[...]
 I think there are cases where this syntax is more readable and there are
 cases where struct initialization is more readable. In the example you
 gave, I do not know what is the second field about and having to mention
 the first field feels like a burden. As developer I want the choice to
 decide between these 2 syntax options.
You can create the attribute separately: enum Field a = { locationName: "B" }; a int c; Or if avoiding the extra symbol is more important than beauty, call a function literal: ((){ Field a = { locationName: "B" }; return a; }()) int c; That's not as succinct as the syntax you propose, of course. But your syntax (` A = {locationName: "B"} int c;`) misses an important detail: There's no indication what the type of the attribute is (or is `A` supposed to be the type?). To make it work, you will have to add that. Something like ` Field(locationName: "B")` or ` Field{locationName: "B"}` or whatever works. At that point, why limit it to attributes? Constructors like that would be nice everywhere. I remember such syntax being discussed repeatedly, but I don't know where we stand. Maybe there's a DIP already?
May 12 2017
parent Andre Pany <andre s-e-a-p.de> writes:
On Friday, 12 May 2017 at 19:33:52 UTC, ag0aep6g wrote:
 On 05/12/2017 09:01 PM, Andre Pany wrote:
 [...]
[...]
  [...]
[...]
 [...]
You can create the attribute separately: enum Field a = { locationName: "B" }; a int c; Or if avoiding the extra symbol is more important than beauty, call a function literal: ((){ Field a = { locationName: "B" }; return a; }()) int c; That's not as succinct as the syntax you propose, of course. But your syntax (` A = {locationName: "B"} int c;`) misses an important detail: There's no indication what the type of the attribute is (or is `A` supposed to be the type?). To make it work, you will have to add that. Something like ` Field(locationName: "B")` or ` Field{locationName: "B"}` or whatever works. At that point, why limit it to attributes? Constructors like that would be nice everywhere. I remember such syntax being discussed repeatedly, but I don't know where we stand. Maybe there's a DIP already?
Yes, A is supposed to be the type. I have included the equal sign to to match the existing struct initializer as far as possible. I would like to limit the dip to increase the chances the dip is accepted. Kind regards André
May 12 2017