struct initializer
Salih Dincer
salihdb at hotmail.com
Fri Dec 1 23:19:32 UTC 2023
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 fun() { return S(5, 2); }
> ```
>
> Or you can use `typeof(return)`:
>
> ```d
> 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:
```d
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 at 79
More information about the Digitalmars-d-learn
mailing list