Jump to page: 1 2
Thread overview
First Draft: Implicit Type Template Instantiation via Constructors
Mar 12
Meta
Mar 12
jmh530
Mar 12
Meta
May 12
Meta
May 14
Meta
May 14
jmh530
May 14
Meta
May 14
Dennis
March 12

This DIP is a partial resurrection of DIP 40 (https://wiki.dlang.org/DIP40) by me and Dennis Korpel. Dennis is working on the implementation (https://github.com/dlang/dmd/pull/16910) while I am writing the DIP.

The purpose of this DIP is to propose a new avenue for Implicit Template Instantiation (ITI) via constructors. Currently (that I'm aware of) D only does Implicit Function Template Instantiation (IFTI); the proposal is to extend this implicit instantiation to types as well, via their constructors.

Before this DIP:

struct Pair(T, U)
{
    T t;
    U u;

    this(T t, U u)
    {
        this.t = t;
        this.u = u;
    }
}

void main()
{
    auto p1 = Pair(1, "asdf"); // Error: struct `Pair` is not callable using argument types `!()(int, string)`
                               // Candidate is: `Pair(T, U)`
}

After this DIP:

struct Pair(T, U)
{
    T t;
    U u;

    this(T t, U u)
    {
        this.t = t;
        this.u = u;
    }
}

void main()
{
    auto p1 = Pair(1, "asdf"); // Okay, T is deduced as int and U as string
}

The DIP:
https://github.com/MetaLang/DIPs/blob/dip1050/DIPs/DIP1050.md

March 12

On Wednesday, 12 March 2025 at 00:20:55 UTC, Meta wrote:

>

This DIP is a partial resurrection of DIP 40 (https://wiki.dlang.org/DIP40) by me and Dennis Korpel. Dennis is working on the implementation (https://github.com/dlang/dmd/pull/16910) while I am writing the DIP.

[...]

Glad to see someone working on this problem.

How would this apply to template aliases (e.g. DIP1023)?

March 12

On Wednesday, 12 March 2025 at 02:37:06 UTC, jmh530 wrote:

>

On Wednesday, 12 March 2025 at 00:20:55 UTC, Meta wrote:

>

This DIP is a partial resurrection of DIP 40 (https://wiki.dlang.org/DIP40) by me and Dennis Korpel. Dennis is working on the implementation (https://github.com/dlang/dmd/pull/16910) while I am writing the DIP.

[...]

Glad to see someone working on this problem.

How would this apply to template aliases (e.g. DIP1023)?

From my understanding, it should work the same way as IFTI, i.e., if eventually this code compiles:

struct TemplateType(T) { }
alias TemplateAlias(T) = TemplateType!T;
void templateFunction(T)(TemplateAlias!T arg) { }

void main()
{
    TemplateAlias!int ta;
    templateFunction(ta);
}

Then this code should as well:

struct TemplateType(T) { }
alias TemplateAlias(T) = TemplateType!T;
struct AnotherTemplateType(T)
{
    this(TemplateAlias!T arg) {}
}

void main()
{
    TemplateAlias!int ta;
    auto att = AnotherTemplateType(ta); // Currently does not compile, even if you add AnotherTemplateType!(TemplateAlias!int)(ta)
}
March 20

On Wednesday, 12 March 2025 at 00:20:55 UTC, Meta wrote:

>

This DIP is a partial resurrection of DIP 40 (https://wiki.dlang.org/DIP40) by me and Dennis Korpel. Dennis is working on the implementation (https://github.com/dlang/dmd/pull/16910) while I am writing the DIP.

The purpose of this DIP is to propose a new avenue for Implicit Template Instantiation (ITI) via constructors. Currently (that I'm aware of) D only does Implicit Function Template Instantiation (IFTI); the proposal is to extend this implicit instantiation to types as well, via their constructors.

Before this DIP:

struct Pair(T, U)
{
    T t;
    U u;

    this(T t, U u)
    {
        this.t = t;
        this.u = u;
    }
}

void main()
{
    auto p1 = Pair(1, "asdf"); // Error: struct `Pair` is not callable using argument types `!()(int, string)`
                               // Candidate is: `Pair(T, U)`
}

After this DIP:

struct Pair(T, U)
{
    T t;
    U u;

    this(T t, U u)
    {
        this.t = t;
        this.u = u;
    }
}

void main()
{
    auto p1 = Pair(1, "asdf"); // Okay, T is deduced as int and U as string
}

The DIP:
https://github.com/MetaLang/DIPs/blob/dip1050/DIPs/DIP1050.md

You spell "Changes" wrong, you better fixed that.

It is a straight up massive miss opportunity to not handle Partial template parameter deduction here. C++ doesn't even handle that edge case which due to that you had to resort to an all or nothing in C++, which can be very annoying at times.

May 12

On Thursday, 20 March 2025 at 18:01:05 UTC, 12345swordy wrote:

>

You spell "Changes" wrong, you better fixed that.

Thank you, fixed.

>

It is a straight up massive miss opportunity to not handle Partial template parameter deduction here. C++ doesn't even handle that edge case which due to that you had to resort to an all or nothing in C++, which can be very annoying at times.

I know, and I agree, but that can be added by a later DIP, and W&A suggested that this DIP be as simple as possible. We hope to expand on this feature further with subsequent proposals; this is just a first step.

May 13

On Wednesday, 12 March 2025 at 00:20:55 UTC, Meta wrote:

>

This DIP is a partial resurrection of DIP 40 (https://wiki.dlang.org/DIP40) by me and Dennis Korpel. Dennis is working on the implementation (https://github.com/dlang/dmd/pull/16910) while I am writing the DIP.

The purpose of this DIP is to propose a new avenue for Implicit Template Instantiation (ITI) via constructors. Currently (that I'm aware of) D only does Implicit Function Template Instantiation (IFTI); the proposal is to extend this implicit instantiation to types as well, via their constructors.

[...]

The DIP:
https://github.com/MetaLang/DIPs/blob/dip1050/DIPs/DIP1050.md

Makes a lot of sense to me.

What about implicit constructors though?

struct Pair(T,U) {
    T t;
    U u;
}
void main() {
    auto p = Pair(1,"string");
}
May 14

On Tuesday, 13 May 2025 at 06:06:54 UTC, Sebastiaan Koppe wrote:

>

On Wednesday, 12 March 2025 at 00:20:55 UTC, Meta wrote:

>

This DIP is a partial resurrection of DIP 40 (https://wiki.dlang.org/DIP40) by me and Dennis Korpel. Dennis is working on the implementation (https://github.com/dlang/dmd/pull/16910) while I am writing the DIP.

The purpose of this DIP is to propose a new avenue for Implicit Template Instantiation (ITI) via constructors. Currently (that I'm aware of) D only does Implicit Function Template Instantiation (IFTI); the proposal is to extend this implicit instantiation to types as well, via their constructors.

[...]

The DIP:
https://github.com/MetaLang/DIPs/blob/dip1050/DIPs/DIP1050.md

Makes a lot of sense to me.

What about implicit constructors though?

struct Pair(T,U) {
    T t;
    U u;
}
void main() {
    auto p = Pair(1,"string");
}

That is not actually a constructor - it is struct literal syntax. I did not realize there was a difference either until recently.

May 14

On Wednesday, 14 May 2025 at 06:46:09 UTC, Meta wrote:

>

On Tuesday, 13 May 2025 at 06:06:54 UTC, Sebastiaan Koppe wrote:

>

On Wednesday, 12 March 2025 at 00:20:55 UTC, Meta wrote:

>

This DIP is a partial resurrection of DIP 40 (https://wiki.dlang.org/DIP40) by me and Dennis Korpel. Dennis is working on the implementation (https://github.com/dlang/dmd/pull/16910) while I am writing the DIP.

The purpose of this DIP is to propose a new avenue for Implicit Template Instantiation (ITI) via constructors. Currently (that I'm aware of) D only does Implicit Function Template Instantiation (IFTI); the proposal is to extend this implicit instantiation to types as well, via their constructors.

[...]

The DIP:
https://github.com/MetaLang/DIPs/blob/dip1050/DIPs/DIP1050.md

Makes a lot of sense to me.

What about implicit constructors though?

struct Pair(T,U) {
    T t;
    U u;
}
void main() {
    auto p = Pair(1,"string");
}

That is not actually a constructor - it is struct literal syntax. I did not realize there was a difference either until recently.

Fair enough, but could the dip support it, or perhaps in the future? The fact that we both expected it was a constructor - and you could argue it actually is, on the grounds that it does in fact construct an object - suggests more users would expect it to work.

May 14

On Wednesday, 14 May 2025 at 16:41:44 UTC, Sebastiaan Koppe wrote:

>

[snip]

>

That is not actually a constructor - it is struct literal syntax. I did not realize there was a difference either until recently.

Fair enough, but could the dip support it, or perhaps in the future? The fact that we both expected it was a constructor - and you could argue it actually is, on the grounds that it does in fact construct an object - suggests more users would expect it to work.

Struct literal syntax only works if there are no constructors. So at (current) step 4, you would want to check if there are no constructors. But then it might be a little more complicated to actually handle that case.

May 14

On Wednesday, 14 May 2025 at 16:41:44 UTC, Sebastiaan Koppe wrote:

>

On Wednesday, 14 May 2025 at 06:46:09 UTC, Meta wrote:

>

On Tuesday, 13 May 2025 at 06:06:54 UTC, Sebastiaan Koppe wrote:

>

On Wednesday, 12 March 2025 at 00:20:55 UTC, Meta wrote:

>

This DIP is a partial resurrection of DIP 40 (https://wiki.dlang.org/DIP40) by me and Dennis Korpel. Dennis is working on the implementation (https://github.com/dlang/dmd/pull/16910) while I am writing the DIP.

The purpose of this DIP is to propose a new avenue for Implicit Template Instantiation (ITI) via constructors. Currently (that I'm aware of) D only does Implicit Function Template Instantiation (IFTI); the proposal is to extend this implicit instantiation to types as well, via their constructors.

[...]

The DIP:
https://github.com/MetaLang/DIPs/blob/dip1050/DIPs/DIP1050.md

Makes a lot of sense to me.

What about implicit constructors though?

struct Pair(T,U) {
    T t;
    U u;
}
void main() {
    auto p = Pair(1,"string");
}

That is not actually a constructor - it is struct literal syntax. I did not realize there was a difference either until recently.

Fair enough, but could the dip support it, or perhaps in the future? The fact that we both expected it was a constructor - and you could argue it actually is, on the grounds that it does in fact construct an object - suggests more users would expect it to work.

I was going to say that this is not possible without at least 1 defined constructor due to how the implementation works. However, look at step 5 of the algorithm in the DIP:

5. If a matching constructor is found (in this case, this(T t, U u), create a new constructor this(T, U)(T t, U u) and try IFTI with that.

If it's creating a new constructor anyway to be instantiated, it could probably do this for structs even when there are no explicit constructors defined... I'll see what Dennis thinks want this and whether it's worth doing, because it should be very simple to add.

« First   ‹ Prev
1 2