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 = S(5, 3); // works
s = S(6, 2); // works
S fun() { return S(5, 2); } // works
int fun2(S s2);
fun2(S(4,4)); // works
```

but

```d
struct S { int a; int b; }

S s = { 5, 3 }; // works
s = { 6, 2 }; // doesn't work
S fun() { return { 5, 2 }; } // doesn't work
int fun2(S s2);
fun2(S(4,4)); // doesn't work
```

So, why supporting the (somewhat strange looking) version with 
curly backets at all?
It only works in one special place, so is simply overhead to 
remember.
Again a superfluous way to do the same - but only under specific 
circumstances.


I think a syntax should work either always or never.


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 version is that supports it, but I 
know the example I posted works in 2.105. It doesn't require any 
compiler options.


Named arguments are still a work in progress, and there are some 
situations where they aren't available (for example, I don't 
think they work for templates yet). With struct literals, though, 
you shouldn't have any problems using them.


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; int b; }

S3 fun() { return S3(5, 2); }

The language spec calls this a [struct literal][1]. If you're 
using a new enough compiler, it even supports named arguments:


S3 fun2() { return S3(b: 2, a: 5); }

[1]: https://dlang.org/spec/struct.html#struct-literal


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 explicit 
constructor

```

Is there a reason why the short form is not possible?
It's clearly an initialization of a new instance of a struct, and 
the requested type is unambigous (the return type of the 
function).


Re: mixin issue

2023-11-29 Thread Paul Backus via Digitalmars-d-learn

On Wednesday, 29 November 2023 at 13:31:14 UTC, DLearner wrote:

```
Error: found `End of File` when expecting `;` following 
statement

```

If an extra ; is added:
```
   }(` ~ strStartPtr ~ `,` ~ strPLPtr ~ `);`;
```

it works but doesn't seem correct.


This is an annoying limitation of the D compiler.

The root of the problem is that there is an ambiguity in D's 
grammar. When the compiler sees


mixin(whatever);

...it cannot tell whether it's supposed to be a [Mixin 
Statement][1], or an [Expression Statement][2] that contains a 
[Mixin Expression][3].


If it's a Mixin Statement, then there should be a semicolon 
inside the mixin. If it's an Expression Statement with a Mixin 
Expression, then there shouldn't be a semicolon inside the mixin.


To resolve this ambiguity, the compiler (currently) *assumes* 
that it's always a Mixin Statement, which means that it will 
always require a semicolon inside the mixin.


As a result, it is impossible to write a string mixin that can be 
used as both a statement and an expression.


If you have an expression mixin and you would like to use it as a 
statement, you can work around this limitation by adding 
`cast(void)` in front of the mixin:


cast(void) mixin(whatever);

This forces the compiler to parse the line as an Expression 
Statement containing a [Cast Expression][4], but does not 
otherwise change the meaning of the code.


[1]: https://dlang.org/spec/statement.html#mixin-statement
[2]: https://dlang.org/spec/statement.html#expression-statement
[3]: https://dlang.org/spec/expression.html#mixin_expressions
[4]: https://dlang.org/spec/expression.html#cast_expressions


Re: mixin issue

2023-11-29 Thread Dennis via Digitalmars-d-learn

On Wednesday, 29 November 2023 at 13:31:14 UTC, DLearner wrote:

it works but doesn't seem correct.


You're mixing in an expression that creates an empty function and 
calls it. What do you want it to do?




mixin issue

2023-11-29 Thread DLearner via Digitalmars-d-learn

The code:

```
void main() {

   struct SB {
  char  SBChrFld1;
  char  SBChrFld2;
  int   SBIntFld1;
  int   SBIntFld2;
   }
   SB  SBVar;

   SB* wkSBPtr;


   void* StartPtr1 = null;

   mixin(mxnDelMbr("StartPtr1", "wkSBPtr"));
   return;
}

string mxnDelMbr()(string strStartPtr, string strPLPtr) {
   return `(void* StartPtr, typeof(` ~ strPLPtr ~ `) PLPtr) {

   }(` ~ strStartPtr ~ `,` ~ strPLPtr ~ `)`;
}
```
Fails with:

```
Error: found `End of File` when expecting `;` following statement
```

If an extra ; is added:
```
   }(` ~ strStartPtr ~ `,` ~ strPLPtr ~ `);`;
```

it works but doesn't seem correct.
Further, example above cloned from several other similar 
examples, which
not only work, but if ';' introduced in corresponding place, dmd 
complains with

empty statement deprecation.
FWIW only difference identified between successful examples and 
this one,

is that other functions all return something, this one does not.

Suggestions?