December 01

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.

December 01

On Friday, 1 December 2023 at 13:02:06 UTC, Dom DiSc wrote:

>
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.

December 01

On Friday, 1 December 2023 at 13:02:06 UTC, Dom DiSc wrote:

>
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 auto:

auto fun() { return S(5, 2); }

Or you can use typeof(return):

SomeReallyLongReturnType fun()
{
    return typeof(return)(5, 2);
}
December 01

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] remain silent about it.

commit 42609ae98e0f72a8d2154da50865bc5182c4b6b3
Author: Dennis <d[...]
Date:   Tue Jan 17 13:08:30 2023 +0100

    Add named arguments to struct literals (#14776)

    * Add named argument parsing

[...]

The named parameters are not restricted to struct constructors:

import std.stdio;

auto delta (int from, int to)
{
   return to - from;
}

void main ()
{
   writeln (delta (to: 8, from: 1));
}
$ dmd structinit.d
$ ./structinit
7

[1] https://dlang.org/changelog/2.103.0.html

December 01

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 the very first instinct I'd have is to replace the first version by the second to reduce verbosity and eliminate an unnecessary copy.

However, for some reason the compiler is not able to deduce the type in the second case, so I'm out of luck.

I'm glad to hear that, with a compiler update, I will at least be able to do

s_list ~= S( a:1, b:2 );

instead of

s_list ~= S( 1, 2 );

but it still seems very inconsistent.

December 01

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:

auto fun() { return S(5, 2); }

Or you can use typeof(return):

SomeReallyLongReturnType fun()
{
    return typeof(return)(5, 2);
}

Paul's example is very readable and short so it's nice. Moreover, when there are named parameters in D, what is the need for them. While there is so much convenience in D, getting stuck on a very simple option...

You decide:

struct Point
{
  int x, y;

  auto opBinary(string  op : "-")(Point that)
  => Point(this.x - that.x, this.y - that.y);

  auto opBinary(string  op : "+")(Point that)
  => Point(this.x + that.x, this.y + that.y);

  auto opBinary(string  op : "*")(Point that)
  => Point(this.x * that.x, this.y * that.y);
  // Moreover, it was possible to do this trio
  // at once with mixin...
}

void main()
{
  auto upperRightCorner = Point(y:768);
  Point x =  { 10, 268  };

  import std.stdio : dout = writeln;

  dout(upperRightCorner - x);
  dots(a: upperRightCorner, b: x).dout;
}

alias dots = differenceOfTwoSquares;
auto differenceOfTwoSquares(Point a, Point b)
  => (a - b)*(a + b);

/*
 * dmd -run "namedParameters.d"
 * Point(-10, 500)
 * Point(-100, 518000)
 */

SDB@79

1 2
Next ›   Last »