digitalmars.D - Concerns about using struct initializer in UDA?
- Andre Pany (21/21) May 11 2017 Hi,
- Stefan Koch (2/23) May 11 2017 We have that syntax already.
- Andre Pany (6/36) May 11 2017 I do not understand. Should the syntax I have written already
- Stefan Koch (6/16) May 11 2017 I thought it should have worked already.
- Andre Pany (7/25) May 12 2017 Does it make sense to open a DIP? It would make UDA quite nice to
- Seb (6/10) May 12 2017 The entire point of a DIP is to do this research for everyone and
- Andre Pany (8/20) May 12 2017 If I find time I will create a new dip. The dip you mentioned is
- Nicholas Wilson (14/35) May 12 2017 I'm not sure if it isi what you're looking for but you can do
- Andre Pany (9/51) May 12 2017 I think there are cases where this syntax is more readable and
- ag0aep6g (18/36) May 12 2017 [...]
- Andre Pany (7/30) May 12 2017 Yes, A is supposed to be the type. I have included the equal sign
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
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
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: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é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
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: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.On Thursday, 11 May 2017 at 10:49:58 UTC, Andre Pany wrote: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é[...]We have that syntax already.
May 11 2017
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: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éOn Thursday, 11 May 2017 at 10:51:09 UTC, Stefan Koch wrote: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.On Thursday, 11 May 2017 at 10:49:58 UTC, Andre Pany wrote: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é[...]We have that syntax already.
May 12 2017
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
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: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é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
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
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: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é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
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
On Friday, 12 May 2017 at 19:33:52 UTC, ag0aep6g wrote:On 05/12/2017 09:01 PM, Andre Pany wrote: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é[...][...][...][...][...]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