February 05, 2022

On Friday, 4 February 2022 at 10:53:45 UTC, Mike Parker wrote:

>

Discussion Thread

This is the discussion thread for the first round of Community
Review of DIP 1043, "Shortened Method Syntax":

https://github.com/dlang/DIPs/blob/5a3b437f2b6be8fb23e855fc0da20f19f68edac8/DIPs/DIP1043.md

Just to ensure, the new simplified method syntax is only for single assignment methods, right?

I haven't detail studied the AssigmentStatement grammar but is there any possibility that the new syntax can be abused? For example starting new entire statement blocks and that way creating a new ugly way of writing methods.

The reason not allowing the lamda syntax was to have a single way of writing methods. If we allow the new syntax, the we need restrictions for that one as well so that it doesn't become a Frankenstein.

February 06, 2022

On Saturday, 5 February 2022 at 22:42:35 UTC, IGotD- wrote:

>

On Friday, 4 February 2022 at 10:53:45 UTC, Mike Parker wrote:

>

Discussion Thread

This is the discussion thread for the first round of Community
Review of DIP 1043, "Shortened Method Syntax":

https://github.com/dlang/DIPs/blob/5a3b437f2b6be8fb23e855fc0da20f19f68edac8/DIPs/DIP1043.md

Just to ensure, the new simplified method syntax is only for single assignment methods, right?

I haven't detail studied the AssigmentStatement grammar but is there any possibility that the new syntax can be abused? For example starting new entire statement blocks and that way creating a new ugly way of writing methods.

The reason not allowing the lamda syntax was to have a single way of writing methods. If we allow the new syntax, the we need restrictions for that one as well so that it doesn't become a Frankenstein.

I guess you can abuse it like

auto func(int a, int b) => (a,b)
{
    int c,d;
    return a+b+c+d;
}(a, b);

Basically you can write a regular function body if you enclose it inside a lamba and call it immediately.

February 06, 2022

On Saturday, 5 February 2022 at 16:19:42 UTC, Doigt wrote:

>

On Saturday, 5 February 2022 at 14:22:07 UTC, Paul Backus wrote:

>

On Saturday, 5 February 2022 at 14:13:50 UTC, Doigt wrote:

>

On Saturday, 5 February 2022 at 00:17:23 UTC, bauss wrote:

>
void foo() { ... }
void bar() => foo(); // This

private int _value;
void baz(int value) => _value = value; // And this
int baz() => _value;

If it's supported then I'm all for this, but if not then I think it's only a half-baked implementation.

I completely agree. Coming from C# where lambda notation on void is allowed, it seems weird to me that it doesn't always work no matter what kind of method you're using it on. If anything it introduces an inconsistency in the syntax and makes it very inconvenient because it also introduces style inconsistency where if your code uses single line functions side by side and some use the arrow notation and others don't because it's not possible by an arbitrary opinion that it shouldn't be possible. To me such thinking seems rooted in the past.

=> expression is shorthand for { return expression; }, and the type of the expression _value = value is int, not void, so you get a type mismatch. Seems pretty straightforward to me. If anything, adding a special case for void functions would make it less consistent and more arbitrary.

Maybe there's an argument to be made that the compiler should allow this kind of return statement in void functions in general, regardless of whether you use the short or long syntax. But that's a totally separate issue from this DIP's proposal.

Except commonly, and to take two popular examples: JavaScript and C#, the arrow notation is not understood to be a shorthand for a return expression but just an expression. I don't think I'll use this new feature if I can't make my code base consistent in style. It makes no sense to me to have things like
int myFunc (int a) => something;
void myProc (int a) { something; }
it looks bad and it makes it seems like void is a special case that is not immediately obvious why the arrow notation is not used.

Yep, exactly. You'll end up with two different writing styles everywhere, which makes everything look inconsistent.

February 08, 2022

On Saturday, 5 February 2022 at 16:19:42 UTC, Doigt wrote:

>

Except commonly, and to take two popular examples: JavaScript and C#, the arrow notation is not understood to be a shorthand for a return expression but just an expression.

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/lambda-expressions#expression-lambdas:

"An expression lambda returns the result of the expression..."

>

I don't think I'll use this new feature if I can't make my code base consistent in style. It makes no sense to me to have things like
int myFunc (int a) => something;
void myProc (int a) { something; }

The former is equivalent to int myFunc (int a) { return something; }. The latter - void myProc (int a) { something; return; }.

>

it looks bad and it makes it seems like void is a special case that is not immediately obvious why the arrow notation is not used.

February 08, 2022

On Tuesday, 8 February 2022 at 09:31:04 UTC, Max Samukha wrote:

>

On Saturday, 5 February 2022 at 16:19:42 UTC, Doigt wrote:

>

Except commonly, and to take two popular examples: JavaScript and C#, the arrow notation is not understood to be a shorthand for a return expression but just an expression.

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/lambda-expressions#expression-lambdas:

"An expression lambda returns the result of the expression..."

>

I don't think I'll use this new feature if I can't make my code base consistent in style. It makes no sense to me to have things like
int myFunc (int a) => something;
void myProc (int a) { something; }

The former is equivalent to int myFunc (int a) { return something; }. The latter - void myProc (int a) { something; return; }.

>

it looks bad and it makes it seems like void is a special case that is not immediately obvious why the arrow notation is not used.

You cannot use MSDN as a source to counteract my argument because I mentioned "commonly" in reference to popular usage. You'd need a statistic to address that argument. But even ignoring that, if you take a few moments to lower your eyes, you'll see exactly that they also have a case for lambda expressions with no return values. This is actually fatal to you because you could've argued semantics and moved the goal post smoothly without me noticing. But semantics aside, it is clear to me and any one who is intellectually honest that what Microsoft refers to "statement expression" is the simple no return expression I was talking about earlier. Also you never addressed the JS side of my argument. You'll notice that documentation you linked has several types of arrow notations which links back to my point about them that there is not just one use for them.

February 08, 2022
On Tuesday, 8 February 2022 at 17:03:25 UTC, Doigt wrote:
> you'll see exactly that they also have a case for lambda expressions with no return values

Which example is that? Do they assign it to a value of a different type?

> Also you never addressed the JS side of my argument.

D allows a no-return lambda, but it doesn't allow you to assign it to a variable of a different type.

Javascript doesn't do static type checking at all, so obviously it would be different.
February 08, 2022
On 2/4/22 20:44, Paul Backus wrote:
> On Friday, 4 February 2022 at 17:59:40 UTC, Timon Gehr wrote:
>> On 04.02.22 18:18, Tejas wrote:
>>> On Friday, 4 February 2022 at 14:46:39 UTC, Paul Backus wrote:
>>>>     this(string y) => this(0, y);
>>>>     this(string y) { this(0, y); }
>>> ...
>>
>> If you have to violate common style guidelines in order to support a claim that the status quo is not so much worse, then probably the new syntax is actually significantly better. (Razvan did the same in the feedback thread, I don't get why people are doing this. Would this really fly, e.g., in Phobos?)
> 
> It's not super common, but it does show up. Some examples:
> 
> https://github.com/dlang/phobos/blob/v2.098.1/std/algorithm/setops.d#L901
> https://github.com/dlang/phobos/blob/v2.098.1/std/algorithm/iteration.d#L2051-L2052 
> 
> https://github.com/dlang/phobos/blob/v2.098.1/std/encoding.d#L487-L549
> https://github.com/dlang/phobos/blob/v2.098.1/std/datetime/systime.d#L9564-L9574 
> 
> https://github.com/dlang/phobos/blob/v2.098.1/std/math/exponential.d#L2748-L2755 
> 
> https://github.com/dlang/phobos/blob/v2.098.1/std/experimental/allocator/building_blocks/stats_collector.d#L199-L200 
> 

Thanks! I guess one way to interpret this is that there should be shortened method syntax to replace those. ;)
February 09, 2022
On Tuesday, 8 February 2022 at 17:11:19 UTC, Adam D Ruppe wrote:
> On Tuesday, 8 February 2022 at 17:03:25 UTC, Doigt wrote:
>> you'll see exactly that they also have a case for lambda expressions with no return values
>
> Which example is that? Do they assign it to a value of a different type?
>

I think the whole argument is that this will work in C#:

```d
int Value
{
  get => _value;
  set => _value = value; // This is okay
}
```

But it won't work in D according to this DIP:

```d
int value() => _value;
void value(int newValue) => _value = newValue; // This will not work
```

So instead everything becomes inconsistent like:

```d
int value() => _value;
void value(int newValue) { _value = newValue; } // This is ok obv.
```

However, now the code looks inconsistent and not intuitive at all.
February 09, 2022
On Wednesday, 9 February 2022 at 07:54:20 UTC, bauss wrote:
> On Tuesday, 8 February 2022 at 17:11:19 UTC, Adam D Ruppe wrote:
>> On Tuesday, 8 February 2022 at 17:03:25 UTC, Doigt wrote:
>>> you'll see exactly that they also have a case for lambda expressions with no return values
>>
>> Which example is that? Do they assign it to a value of a different type?
>>
>
> I think the whole argument is that this will work in C#:
>
> ```d
> int Value
> {
>   get => _value;
>   set => _value = value; // This is okay
> }
> ```
>
> But it won't work in D according to this DIP:
>
> ```d
> int value() => _value;
> void value(int newValue) => _value = newValue; // This will not work
> ```
>

I don't understand why the fact that C# has a special case for setters is relevant. Even though the setter in C# has 'void' return value, the actual return type of the assignment is still int. Consider:

var y = x.Value = 42;

which the compiler rewrites to something like:

x.set_Value(42);
var y = x.get_Value();

In D, you just return the new value from the setter:

int value(int newValue) => _value = newValue;

> So instead everything becomes inconsistent like:
>
> ```d
> int value() => _value;
> void value(int newValue) { _value = newValue; } // This is ok obv.
> ```
>
> However, now the code looks inconsistent and not intuitive at all.

I disagree. The proposed feature makes D overall more consistent.

February 09, 2022
On Wednesday, 9 February 2022 at 10:34:57 UTC, Max Samukha wrote:
>
> I disagree. The proposed feature makes D overall more consistent.

Yes it does, but not if both cases aren't supported.

And you shouldn't return the value from a setter, it should be void.

Which looks better?

```
int value() => _value;
void value(int newValue) => _value = newValue;
```

Or

```
int value() => _value;
void value(int newValue) { _value = newValue; }
```

The first case definitely looks better IMHO.

And yeah as you said void could just be int, but it shouldn't. The setter should be void.

It is almost universally accepted that setters should __never__ return anything and there's only one case where you might return from a setter and that is in fluid programming where you return the parent object to allow chaining.

So it's irrelevant that it works as a work-around.