September 07, 2021

On 9/7/21 1:01 AM, Tejas wrote:

>

On Tuesday, 7 September 2021 at 01:34:01 UTC, Basile B. wrote:

>

On Wednesday, 1 September 2021 at 04:40:37 UTC, Виталий Фадеев wrote:

>

I want say "Thank you!" to all who works under D.

I see the result of your work, even if it is small, even the size of a byte.
I use D and see your gift.
I like. :)

while this code looks natural and obvious, although pointless

module m;

struct S { int i; }

S _s;

S s (){return _s;}

void main()
{
    s().i = 42;
}

the reality is that i can only be accessed through a pointer to a S (you want a deref of the address plus a offset).but s returns a S by value... so a temporary is created.

All the time we write code that compiles but this code just compiles because the front end will help us, in this case, by turning s() into a lvalue.

so we can indeed say thanks. what you think just works actually requires some unexpected rewrites, like all the time.

Don't you think it would be better to raise a warning though instead of silently fixing it?

It would help people understand the differences between structs and classes better, and when they will be using them in a much more complex set of circumstances where the compiler won't be able to detect what they're doing, they'll use them the right way.

This seems like setting them up for frustration in the future, since one can definitely run into a case where struct will purely be treated as a value type and then the user will feel that the behaviour is inconsistent with what they've experienced.

This reminds me of single length string literals being treated as a character.

void main(){
      char[5] a = "y";//shouldn't compile, but it does

That is certainly an odd behavior. Looking at the resulting array, it's padded with 0s.

>

     char[5] b = 'y';
     writeln(a);
     writeln(b);
}

Output:
y
yyyyy

This is not doing what you think it is doing. It's outputting essentially "y\0\0\0\0", you just don't see the 0's on the console.

>

Was it a good thing that the compiler rewrote the string into a char in this case? Wouldn't it be better if an error/warning was provided instead?

I think this "feature" should be removed, personally. Especially since it's not consistent with other array types.

-Steve

September 07, 2021

On Tuesday, 7 September 2021 at 08:46:48 UTC, Basile B. wrote:

>

On Tuesday, 7 September 2021 at 05:01:30 UTC, Tejas wrote:

>

On Tuesday, 7 September 2021 at 01:34:01 UTC, Basile B. wrote:

>

[...]

Don't you think it would be better to raise a warning though instead of silently fixing it?

no. as compiler experiment people can add a flag to generated temproaries and then printf on a visitor for VarExp and then will probabably be surprised by the amount of message printf'd like 2567 times

"look boy am saving expressivity for ya here"

>

It would help people understand the differences between [...]

Can you please show me how to do it? I don't know of the cmd line arg that does this (vgc-ast? But then how do you make it print certain nodes?)

September 07, 2021

On Tuesday, 7 September 2021 at 05:36:17 UTC, jfondren wrote:

>

On Tuesday, 7 September 2021 at 05:09:39 UTC, jfondren wrote:

>

On Tuesday, 7 September 2021 at 05:01:30 UTC, Tejas wrote:

>

This reminds me of single length string literals being treated as a character.

void main(){
     char[5] a = "y";//shouldn't compile, but it does
     char[5] b = 'y';
     writeln(a);
     writeln(b);
}


Output:
y
yyyyy

Was it a good thing that the compiler rewrote the string into a char in this case? Wouldn't it be better if an error/warning was provided instead?

This example works with a initialized to "yy" as well, or "yyxx". The chars not provided are initialized to 0 (rather than 255 if the variable weren't initialized at all). This seems like a nice way to reuse a large fixed buffer that initially has small relevant content, like char[255] line = "hello";

It doesn't initialize the entire array to 'y', so it's not treating "y" as the same as 'y'.

Seems strange to only permit this with a string literal though.

unittest {
    char[3] a;
    char[2] b = '!';

    // this is OK
    a = "!!";
    assert(a == [33, 33, 0]);

    // Error: mismatched array lengths, 3 and 2
    assert(!__traits(compiles, a = ['!', '!']));
    assert(!__traits(compiles, { a[] = b[]; }));
}

You can work around this problem with designated initializers

void main(){
    char[3] a = [0:'!', /*no need for "1:"*/'!', 2:char.init/*you  MUST assign explicitly to the last member of the array otherwise you get mismatched length error for some reason. Also note that this is only necessary when you use "0:" */];
    a[0 .. b.length] = b[];//this is the only way since a and b are of different types
}
September 07, 2021

On Tuesday, 7 September 2021 at 08:46:48 UTC, Basile B. wrote:

>

On Tuesday, 7 September 2021 at 05:01:30 UTC, Tejas wrote:

>

On Tuesday, 7 September 2021 at 01:34:01 UTC, Basile B. wrote:

>

[...]

Don't you think it would be better to raise a warning though instead of silently fixing it?

no. as compiler experiment people can add a flag to generated temproaries and then printf on a visitor for VarExp and then will probabably be surprised by the amount of message printf'd like 2567 times

"look boy am saving expressivity for ya here"

>

It would help people understand the differences between [...]

import std.stdio;

struct S { int i; }

S _s;

S s (){return _s;}

void main()
{
    s().i = 42;
    writeln(_s.i == 42);
}

Output:
false

Wow, compiler help much appreciated

September 07, 2021

On Tuesday, 7 September 2021 at 13:36:39 UTC, Tejas wrote:

>
import std.stdio;

struct S { int i; }

S _s;

S s() {return _s;}

void main()
{
    s().i = 42;
    writeln(_s.i == 42);
}

Output:
false

Wow, compiler help much appreciated

Is this confusing to you? I might be missing your point. It isn’t confusing to me, structs are value types. If you want s() to return a reference to _s it should be declared as returning a reference:

import std.stdio;

struct S { int i; }

S _s;

ref S s() {return _s;}

void main()
{
    s().i = 42;
    writeln(_s.i == 42);
}

Output:
true

—Bastiaan.

September 07, 2021

On Monday, 6 September 2021 at 19:35:19 UTC, IGotD- wrote:

>

We need developers, developers, developers, developers, developers. Like that cringe Steve Ballmer moment. I don't know what Steve Ballmer is doing today but he is not at Microsoft anymore. Maybe we should hire him.

D has/had numerous developers very few of us could rival. I definitely notice a lot less of the "elite" here than I used to in the 2014-2016ish period. I've also noticed a decline in talk about the language's future direction on this forum, as I assume the Slack and Discord is where most of that is happening now.

I wonder where all the action is now. I've said before that these forums used to have something exciting posted about the language's direction pretty at a relatively often pace, but now it seems to be a special occasion when something noteworthy is announced.

Hopefully it's all being saved for DConf >:3

/incoherent_unstructured_nonsense

September 07, 2021

On Tuesday, 7 September 2021 at 22:48:23 UTC, SealabJaster wrote:

>

On Monday, 6 September 2021 at 19:35:19 UTC, IGotD- wrote:

>

[...]

D has/had numerous developers very few of us could rival. I definitely notice a lot less of the "elite" here than I used to in the 2014-2016ish period. I've also noticed a decline in talk about the language's future direction on this forum, as I assume the Slack and Discord is where most of that is happening now.

I wonder where all the action is now. I've said before that these forums used to have something exciting posted about the language's direction pretty at a relatively often pace, but now it seems to be a special occasion when something noteworthy is announced.

Hopefully it's all being saved for DConf >:3

/incoherent_unstructured_nonsense

That could also be because the language is getting small improvements all the time and focus is on stability and consistency rather than new things.

September 07, 2021

On Tuesday, 7 September 2021 at 23:26:08 UTC, Imperatorn wrote:

>

That could also be because the language is getting small improvements all the time and focus is on stability and consistency rather than new things.

That is very much true.

September 08, 2021

On Tuesday, 7 September 2021 at 21:43:02 UTC, Bastiaan Veelo wrote:

>

On Tuesday, 7 September 2021 at 13:36:39 UTC, Tejas wrote:

>
import std.stdio;

struct S { int i; }

S _s;

S s() {return _s;}

void main()
{
    s().i = 42;
    writeln(_s.i == 42);
}

Output:
false

Wow, compiler help much appreciated

Is this confusing to you? I might be missing your point. It isn’t confusing to me, structs are value types. If you want s() to return a reference to _s it should be declared as returning a reference:

import std.stdio;

struct S { int i; }

S _s;

ref S s() {return _s;}

void main()
{
    s().i = 42;
    writeln(_s.i == 42);
}

Output:
true

—Bastiaan.

The people that don't find this confusing will never even write S alone as a return type in the first place.

Again, I said that this is setting people up for frustration in the future, and I stick to it.

Do you really think people who are writing code like that expect a temporary to be generated? What else could be the purpose of returning an existing struct and using it as an lvalue if not to modify it?

I feel we would be better off if the same

<your_var_name> is not an lvalue and cannot be modified

error that is raised for other similar cases came up in the original version of this code. It will catch a (almost 100% guranteed) runtime error during compile time and be consistent with the rest of the language.

September 07, 2021
On 9/7/21 9:36 PM, Tejas wrote:

>>> S _s;
>>>
>>> S s() {return _s;}

[...]

> Do you really think people who are writing code like that expect a
> temporary to be generated?

I think so. Even if they don't, I can't imagine this being a big surprise in the future; a little test would reveal that _s was not changing.

> What else could be the purpose of returning
> an existing struct and using it as an `lvalue` if not to modify it?

The returned copy is not an lvalue; it is an rvalue.

> I feel we would be better off if the same
> ```d
> <your_var_name> is not an lvalue and cannot be modified
> ```

rvalues cannot be assigned to but they can be modified. The reason I don't find this surprising must be my past experience with C++, where it is exactly the same.

> error that is raised for other similar cases came up in the original
> version of this code. It will catch a (almost 100% guranteed) runtime
> error during compile time _and_ be consistent with the rest of the
> language.

How? Being value types, structs are consistently passed and returned by value. Otherwise, we wouldn't be able to write a factory function:

S makeS() {
  return S();  // Should this be returning a reference to a temporary?
}

Ali