Function Overloading

Basile B. b2.temp at gmx.com
Wed Nov 1 20:04:52 UTC 2023


On Tuesday, 31 October 2023 at 20:09:44 UTC, Salih Dincer wrote:
> [...]
> is it possible to do something like `alias add = this;`?
>
> ```d
> struct Calculate
> {
>     int memory;
>     string result;
>
>     auto toString() => result;
>
>     // alias add = this;
>     
>     import std.string : format;
>
>     this(string str)
>     {
>         result ~= str.format!"%s + ";
>     }
>
>     this(int num)
>     {
>         memory += num;
>         add(num.format!"%s");
>     }
> }
> ```
>
> SDB at 79

Yes. D constructors are not named but the current implementation 
adds a name that is `__ctor`, so add

```d
alias add = __ctor;
```

to you struct.

Note that however this kind of internal details should not be 
used, even if chances that this name changes are low (old 2014 
code that uses it still compiles and have never broke, phobos 
uses it too IIRC).

Otherwise take care to copy construction, that wont work with the 
alias.


More information about the Digitalmars-d-learn mailing list