```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 explic
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; i
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?
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 ver
Sorry, I meant
```d
fun2({4, 4}); // doesn't work
```
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 = S
s.
Walter tried 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) st
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.
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
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
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.
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.
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 `aut
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
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 and
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 fu
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;
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
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 of
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 i
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 u
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
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 alr
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.
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
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
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
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,
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,
29 matches
Mail list logo