Thread overview
Function Overloading
Nov 01
Basile B.
Nov 02
Basile B.
October 31
struct Calculate
{
    int memory;
    string result;

    auto toString() => result;

    this(string str)
    {
        add(str);
    }

    this(int num)
    {
        add(num);
    }

    import std.string : format;

    void add(string str)
    {
        result ~= str.format!"%s + ";
    }

    void add(int num)
    {
        memory += num;
        add(num.format!"%s");
    }
}

import std.stdio;
void main()
{
    // without constructor:
    Calculate c;
    c.add("x");
    c.add(2);
    c.writeln; // x + 2 +
    
    // with constructor:
    c = Calculate("x");
    c.add(2);
    c.writeln; // x + 2 +
}

There is a simple struct like the one above that is not related to reality. There can be more than 2 function overloading in it, but in our example there are only 2. Without implementing a similar function again for each constructor; is it possible to do something like alias add = this;?

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@79

November 01

On Tuesday, 31 October 2023 at 20:09:44 UTC, Salih Dincer wrote:

>

[...]
is it possible to do something like alias add = this;?

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@79

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

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.

November 02

On Wednesday, 1 November 2023 at 20:04:52 UTC, Basile B. wrote:

>

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

alias add = __ctor;

to you struct.

Yeah, it works! Thanks...:)

SDB@79

November 02

On Thursday, 2 November 2023 at 11:17:42 UTC, Salih Dincer wrote:

>

On Wednesday, 1 November 2023 at 20:04:52 UTC, Basile B. wrote:

>

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

alias add = __ctor;

to you struct.

Yeah, it works! Thanks...:)

SDB@79

I'm no sure how this could be used IRL but keep well in mind that this is not nice code.

My answer should be more interepreted as "yes that is technically possible". ("can do" -> yes VS "should do" -> no).

You also have the opCall() option btw.

November 03

On Tuesday, 31 October 2023 at 20:09:44 UTC, Salih Dincer wrote:

>
struct Calculate
{
    int memory;
    string result;

    auto toString() => result;

    this(string str)
    {
        add(str);
    }

    this(int num)
    {
        add(num);
    }

    import std.string : format;

    void add(string str)
    {
        result ~= str.format!"%s + ";
    }

    void add(int num)
    {
        memory += num;
        add(num.format!"%s");
    }
}

import std.stdio;
void main()
{
    // without constructor:
    Calculate c;
    c.add("x");
    c.add(2);
    c.writeln; // x + 2 +
    
    // with constructor:
    c = Calculate("x");
    c.add(2);
    c.writeln; // x + 2 +
}

There is a simple struct like the one above that is not related to reality. There can be more than 2 function overloading in it, but in our example there are only 2. Without implementing a similar function again for each constructor; is it possible to do something like alias add = this;?

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@79

Can you modify your code with the below code I hope you can get what you are looking for.

struct Calculate
{
    int memory;
    string result;

    auto toString() => result;

    private void addCommon(string str)
    {
        result ~= str.format!"%s + ";
    }

    import std.string : format;

    this(string str)
    {
        addCommon(str);
    }

    this(int num)
    {
        memory += num;
        addCommon(num.format!"%s");
    }
}

Thanks