Jump to page: 1 2
Thread overview
September 19

As an idea this has come up in Razvan's DConf 2024 talk.

  1. Support inheritance on struct, for other structs.
struct Parent {
    ...
}

struct Child : Parent {
    ...
}
  1. opDispatch function, may work in place of alias this when no parent exists.
struct Parent {
    T thing;
    ref T opDispatch(string:"")() {
        return this.thing;
    }
}

struct Child : Parent {
}

Child child;
T got = child;
September 19

On Thursday, 19 September 2024 at 15:56:06 UTC, Richard (Rikki) Andrew Cattermole wrote:

>

As an idea this has come up in Razvan's DConf 2024 talk.

  1. Support inheritance on struct, for other structs.
struct Parent {
    ...
}

struct Child : Parent {
    ...
}

In the talk, Walter mentioned doing that instead of having alias this. That would be much less powerful. alias this allows hooking conversion to the target type. With alias this = func; T func();, you can create the T instance at runtime. It doesn't even have to be the same instance each time. Also T doesn't have to be a struct.

>
  1. opDispatch function, may work in place of alias this when no parent exists.
struct Parent {
    T thing;
    ref T opDispatch(string:"")() {
        return this.thing;
    }
}

struct Child : Parent {
}

Child child;
T got = child;

This seems like alias this in disguise.

September 20
On 20/09/2024 6:57 AM, Nick Treleaven wrote:
> This seems like |alias this| in disguise.

The problem with alias this, is it is not defined as the fallback when nothing else in the hierarchy matches.

Here it is better defined, opDispatch should only be chosen after exhausting the hierarchy, both in classes and structs.

Eliminating the issues with alias this from the design.

We can further restrict it, to only support the parent most type in the hierarchy or only one in the hierarchy. In effect it allows adding a new parent to the top of the hierarchy.

September 20
On 20/09/2024 7:04 AM, Richard (Rikki) Andrew Cattermole wrote:
> On 20/09/2024 6:57 AM, Nick Treleaven wrote:
>> This seems like |alias this| in disguise.
> 
> The problem with alias this, is it is not defined as the fallback when nothing else in the hierarchy matches.
> 
> Here it is better defined, opDispatch should only be chosen after exhausting the hierarchy, both in classes and structs.
> 
> Eliminating the issues with alias this from the design.
> 
> We can further restrict it, to only support the parent most type in the hierarchy or only one in the hierarchy. In effect it allows adding a new parent to the top of the hierarchy.

I should mention that this aspect of the proposal while it add new semantics, doesn't have anything special related to the rules of acceptability of opDispatch itself.

We'll need to define how opDispatch works in the hierarchy. Otherwise we'll have come right back to the same set of issues as alias this has.

September 19

On Thursday, 19 September 2024 at 15:56:06 UTC, Richard (Rikki) Andrew Cattermole wrote:

>

As an idea this has come up in Razvan's DConf 2024 talk.

Last time this came up on the forums, I did a quick search through Phobos to see how many uses of alias this forwarded to a member variable (and could be replaced by struct inheritance) and how many forwarded to a function.

My hypothesis before actually checking was that alias this to a member variable would be significantly more common than alias this to a function. But in reality, they turned out to be equally common.

Original post: https://forum.dlang.org/post/nbysytbybvqwgxvdexym@forum.dlang.org

September 20

On Thursday, 19 September 2024 at 15:56:06 UTC, Richard (Rikki) Andrew Cattermole wrote:

>

As an idea this has come up in Razvan's DConf 2024 talk.

  1. Support inheritance on struct, for other structs.
  2. opDispatch function, may work in place of alias this when no parent exists.

What about interfaces?

September 20
On 20/09/2024 10:14 PM, IchorDev wrote:
> On Thursday, 19 September 2024 at 15:56:06 UTC, Richard (Rikki) Andrew Cattermole wrote:
>> As an idea this has come up in Razvan's DConf 2024 talk.
>>
>> 1. Support inheritance on struct, for other structs.
>> 2. ``opDispatch`` function, may work in place of ``alias this`` when no parent exists.
> 
> What about interfaces?

While I do want signatures which would fill this void, the purpose of this is to get rid of alias this which has some problematic behaviors in symbol lookup.
September 20
On Thursday, 19 September 2024 at 19:04:40 UTC, Richard (Rikki) Andrew Cattermole wrote:
> On 20/09/2024 6:57 AM, Nick Treleaven wrote:
>> This seems like |alias this| in disguise.
>
> The problem with alias this, is it is not defined as the fallback when nothing else in the hierarchy matches.
>
> Here it is better defined, opDispatch should only be chosen after exhausting the hierarchy, both in classes and structs.

I don't see why you want to overload opDispatch to do subtyping. It doesn't seem related to existing opDispatch.

> Eliminating the issues with alias this from the design.
>
> We can further restrict it, to only support the parent most type in the hierarchy or only one in the hierarchy. In effect it allows adding a new parent to the top of the hierarchy.

I don't think we need to restrict its capabilities, we just need another syntax for it so we can fix the semantics. If anything I'd like to increase its capabilities. It could be defined as a template method and instantiated with the type of the expected result, and the constraint determines whether the conversion is enabled for that type. In effect, allowing multiple `alias this` with the semantics defined by the aggregate author.
September 21
On 21/09/2024 3:23 AM, Nick Treleaven wrote:
>     Eliminating the issues with alias this from the design.
> 
>     We can further restrict it, to only support the parent most type in
>     the hierarchy or only one in the hierarchy. In effect it allows
>     adding a new parent to the top of the hierarchy.
> 
> I don't think we need to restrict its capabilities, we just need another syntax for it so we can fix the semantics. If anything I'd like to increase its capabilities. It could be defined as a template method and instantiated with the type of the expected result, and the constraint determines whether the conversion is enabled for that type. In effect, allowing multiple |alias this| with the semantics defined by the aggregate author.

Ok so basically implicit conversions with optional target type.

As long as it is defined as being last resort after looking up through the hierarchy, then that aspect I'm ok with. Child most definition only.

You'll need to convince Walter wrt. argument to parameter matching, and he has some strong feelings on this.

But as far as the syntax changes are concerned for no target type, I'm ok with that being swapped to, it'll give it somewhere to expand to in the future.

September 20

On Friday, 20 September 2024 at 15:23:25 UTC, Nick Treleaven wrote:

>

On Thursday, 19 September 2024 at 19:04:40 UTC, Richard (Rikki) Andrew Cattermole wrote:

>

On 20/09/2024 6:57 AM, Nick Treleaven wrote:

>

This seems like |alias this| in disguise.

The problem with alias this, is it is not defined as the fallback when nothing else in the hierarchy matches.

Here it is better defined, opDispatch should only be chosen after exhausting the hierarchy, both in classes and structs.

I don't see why you want to overload opDispatch to do subtyping. It doesn't seem related to existing opDispatch.

>

Eliminating the issues with alias this from the design.

We can further restrict it, to only support the parent most type in the hierarchy or only one in the hierarchy. In effect it allows adding a new parent to the top of the hierarchy.

I don't think we need to restrict its capabilities, we just need another syntax for it so we can fix the semantics. If anything I'd like to increase its capabilities. It could be defined as a template method and instantiated with the type of the expected result, and the constraint determines whether the conversion is enabled for that type.
[...]

It's basically the same as opCast but called without cast. A long time ago this was discussed, (we called that opImpcConvRight). That thing would work but to fix alias this problems then the users need to overload much more operators, and that becomes unatural to use, i.e vs alias this which works more naturally. Fact is there are plenty of situation where implicit convs are not tried but you want that new op to be tried. Basically that's the exact same situations where currently the compiler has to try if alias this is possible.

« First   ‹ Prev
1 2