Re: struct initializer

2023-12-01 Thread Salih Dincer via Digitalmars-d-learn
Hi All, I feel lonely, just as those who come from C++ find it strange, because I think it makes it difficult to read code. On Friday, 1 December 2023 at 14:53:16 UTC, Paul Backus wrote: Technically you don't *have* to repeat the type. You can write the return type as `auto`: ```d auto

Re: struct initializer

2023-12-01 Thread bomat via Digitalmars-d-learn
I completely agree with the OP, and I want to illustrate this by another example which I find quite bizarre: ``` struct S { int a; int b; } S[] s_list = new S[0]; // this works S s = { a:1, b:2 }; s_list ~= s; // this does not s_list ~= { a:1, b:2 }; ``` I'm a C++ programmer in my day job

Re: struct initializer

2023-12-01 Thread kdevel via Digitalmars-d-learn
On Wednesday, 29 November 2023 at 16:48:09 UTC, Paul Backus wrote: [...] If you're using a new enough compiler, it even supports named arguments: S3 fun2() { return S3(b: 2, a: 5); } Indeed. Seems to be in dmd since 2.103.0 (2.102.2 didn't support this syntax). Alas, the Change Log [1]

Re: struct initializer

2023-12-01 Thread Paul Backus via Digitalmars-d-learn
On Friday, 1 December 2023 at 13:02:06 UTC, Dom DiSc wrote: ```d S Fun(){ return { 5, 2 }; } ``` This IS an initialization and the type is known. Requiring the repetition of the type is also here annoying. Technically you don't *have* to repeat the type. You can write the return type as

Re: struct initializer

2023-12-01 Thread zjh via Digitalmars-d-learn
On Friday, 1 December 2023 at 13:02:06 UTC, Dom DiSc wrote: ```d S Fun(){ return { 5, 2 }; } ``` This IS an initialization and the type is known. Requiring the repetition of the type is also here annoying. Right. The `{}` initialization method in C++ is very useful,I like it very much.

Re: struct initializer

2023-12-01 Thread Adam D Ruppe via Digitalmars-d-learn
On Friday, 1 December 2023 at 13:02:06 UTC, Dom DiSc wrote: Either allow it for all initializations, or get rid of it, like DIP 1031 suggested. I thought the decision actually was made to just get rid of it.

Re: struct initializer

2023-12-01 Thread Dom DiSc via Digitalmars-d-learn
On Thursday, 30 November 2023 at 12:15:04 UTC, Dennis wrote: The syntax was inherited from C. The 'special place' is called initialization, and it's special because the target type of the initializer is known in advance This is no different from `S fun(){ return { 5, 2 }; }` It creates a new

Re: struct initializer

2023-12-01 Thread Dom DiSc via Digitalmars-d-learn
On Thursday, 30 November 2023 at 14:10:35 UTC, zjh wrote: On Wednesday, 29 November 2023 at 16:38:36 UTC, Dom DiSc wrote: ```d struct S { int a; int b; } S2 fun3() { return S2( 5, 2 ); } ``` Here,`S2( 5, 2 );` violeit `DRY` principle. Yes. I think if we have the brackets form, it should be

Re: struct initializer

2023-11-30 Thread zjh via Digitalmars-d-learn
On Wednesday, 29 November 2023 at 16:38:36 UTC, Dom DiSc wrote: ```d struct S { int a; int b; } S2 fun3() { return S2( 5, 2 ); } ``` Here,`S2( 5, 2 );` violeit `DRY` principle.

Re: struct initializer

2023-11-30 Thread Dennis via Digitalmars-d-learn
to deprecate the old struct initializer syntax: https://github.com/dlang/DIPs/blob/master/DIPs/other/DIP1031.md But it got some resistance, since {} initializers still have an advantage when you want to define an array of structs, and don't want to repeat the (potentially long) struct name for every entry

Re: struct initializer

2023-11-29 Thread Dom DiSc via Digitalmars-d-learn
On Wednesday, 29 November 2023 at 16:48:09 UTC, Paul Backus wrote: You can use this syntax without an explicit constructor: struct S3 { int a; int b; } S3 fun() { return S3(5, 2); } The language spec calls this a struct literal Ok, so we have ```d struct S { int a; int b; } S s =

Re: struct initializer

2023-11-29 Thread Dom DiSc via Digitalmars-d-learn
Sorry, I meant ```d fun2({4, 4}); // doesn't work ```

Re: struct initializer

2023-11-29 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 29 November 2023 at 17:23:04 UTC, Antonio wrote: On Wednesday, 29 November 2023 at 16:48:09 UTC, Paul Backus wrote: ... it even supports named arguments: - Witch version of DMD supports named arguments? Is it an experimental compiler option? I don't know what the earliest

Re: struct initializer

2023-11-29 Thread Antonio via Digitalmars-d-learn
On Wednesday, 29 November 2023 at 16:48:09 UTC, Paul Backus wrote: ... it even supports named arguments: - Witch version of DMD supports named arguments? Is it an experimental compiler option?

Re: struct initializer

2023-11-29 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 29 November 2023 at 16:38:36 UTC, Dom DiSc wrote: ```d struct S2 { int a; int b; this(int c, int d) { a=c; b=d; } } S2 fun3() { return S2( 5, 2 ); } // works but requires explicit constructor ``` You can use this syntax without an explicit constructor: struct S3 { int a;

struct initializer

2023-11-29 Thread Dom DiSc via Digitalmars-d-learn
```d struct S { int a; int b; } S s = { 5, 2 }; // works fine S fun() { return { 5, 2 }; } // doesn't work :-( S fun2() { S s = { 5, 2 }; return s; } // works but is ugly struct S2 { int a; int b; this(int c, int d) { a=c; b=d; } } S2 fun3() { return S2( 5, 2 ); } // works but requires

Re: Struct initializer in UDA

2020-09-27 Thread realhet via Digitalmars-d-learn
named parameters or the struct initializer. For my use case this opDispatch trick seems to be more flexible than the named-parameters thing: @(FieldProps().range(-360, 360).format("%.2f").caption("Turret rotation").unit("deg")) float alpha = 0; for example if I u

Re: Struct initializer in UDA

2020-09-27 Thread Anonymouse via Digitalmars-d-learn
On Sunday, 27 September 2020 at 10:17:39 UTC, realhet wrote: On Saturday, 26 September 2020 at 17:13:17 UTC, Anonymouse wrote: On Saturday, 26 September 2020 at 16:05:58 UTC, realhet wrote: The closest I can get is @(S.init.c(9).f(42)) with use of opDispatch, which is easier to read but still

Re: Struct initializer in UDA

2020-09-27 Thread realhet via Digitalmars-d-learn
On Saturday, 26 September 2020 at 17:13:17 UTC, Anonymouse wrote: On Saturday, 26 September 2020 at 16:05:58 UTC, realhet wrote: The closest I can get is @(S.init.c(9).f(42)) with use of opDispatch, which is easier to read but still ugly. All I can get is that the - an identifier of a member

Re: Struct initializer in UDA

2020-09-26 Thread Anonymouse via Digitalmars-d-learn
On Saturday, 26 September 2020 at 16:05:58 UTC, realhet wrote: Hi, struct S{int a, b, c=9, d, e, f;} Is there a way or a trick to declare an UDA by using a nice struct initializer? It would be nice to be able to use the form: @S{f:42} int a; //or something similar to this. instead

Struct initializer in UDA

2020-09-26 Thread realhet via Digitalmars-d-learn
Hi, struct S{int a, b, c=9, d, e, f;} Is there a way or a trick to declare an UDA by using a nice struct initializer? It would be nice to be able to use the form: @S{f:42} int a; //or something similar to this. instead of this longer and error-prone way: @S(0, 0, 0, 9, 0, 42) int a;

Re: Why does struct initializer works for arrays but not for associative arrays?

2018-03-14 Thread Andre Pany via Digitalmars-d-learn
On Wednesday, 14 March 2018 at 15:17:54 UTC, Jonathan M Davis wrote: On Wednesday, March 14, 2018 13:36:51 Andre Pany via Digitalmars-d-learn wrote: [...] Well, I think that you have two issues here: 1. Struct literals work in only a few, specific circumstances. Why, I don't know, but IIRC,

Re: Why does struct initializer works for arrays but not for associative arrays?

2018-03-14 Thread Seb via Digitalmars-d-learn
On Wednesday, 14 March 2018 at 15:17:54 UTC, Jonathan M Davis wrote: On Wednesday, March 14, 2018 13:36:51 Andre Pany via Digitalmars-d-learn wrote: [...] Well, I think that you have two issues here: 1. Struct literals work in only a few, specific circumstances. Why, I don't know, but IIRC,

Re: Why does struct initializer works for arrays but not for associative arrays?

2018-03-14 Thread Jonathan M Davis via Digitalmars-d-learn
On Wednesday, March 14, 2018 13:36:51 Andre Pany via Digitalmars-d-learn wrote: > Hi, > > I do not understand why struct initializer works for arrays but > not for > associative arrays: > > struct Bar > { > string s; > } > > struct Foo > { &g

Re: Why does struct initializer works for arrays but not for associative arrays?

2018-03-14 Thread Uknown via Digitalmars-d-learn
On Wednesday, 14 March 2018 at 13:36:51 UTC, Andre Pany wrote: Hi, I do not understand why struct initializer works for arrays but not for associative arrays: struct Bar { string s; } struct Foo { Bar[string] asso; Bar[] arr; } void main() { Foo foo = { arr: [{s

Why does struct initializer works for arrays but not for associative arrays?

2018-03-14 Thread Andre Pany via Digitalmars-d-learn
Hi, I do not understand why struct initializer works for arrays but not for associative arrays: struct Bar { string s; } struct Foo { Bar[string] asso; Bar[] arr; } void main() { Foo foo = { arr: [{s: "123"}], asso: ["0": {s: &quo

Re: alias this and struct initializer

2017-05-18 Thread Andre Pany via Digitalmars-d-learn
On Thursday, 18 May 2017 at 12:56:09 UTC, Adam D. Ruppe wrote: On Thursday, 18 May 2017 at 08:40:39 UTC, Andre Pany wrote: [...] Nope, case 2 is assigning to an already constructed object and case 3 is constructing a new one. [...] Thanks for the explanation, that makes perfectly sense.

Re: alias this and struct initializer

2017-05-18 Thread Adam D. Ruppe via Digitalmars-d-learn
On Thursday, 18 May 2017 at 08:40:39 UTC, Andre Pany wrote: I think as case 2 is working case 3 should work also. Nope, case 2 is assigning to an already constructed object and case 3 is constructing a new one. alias this is NEVER used in construction. It can only apply after the object

alias this and struct initializer

2017-05-18 Thread Andre Pany via Digitalmars-d-learn
Hi, I have some issues with struct initializer and alias this. In following example 1 and 2 is working but there is a syntax error for 3. I think as case 2 is working case 3 should work also. For me case 3 is looking much nicer than case 1. What do you think? void main() { // Working