December 03, 2021

On Friday, 3 December 2021 at 09:35:47 UTC, bauss wrote:

>

On Friday, 3 December 2021 at 04:58:51 UTC, Dave P. wrote:

>

On Friday, 3 December 2021 at 04:25:06 UTC, Elronnd wrote:

>

On Friday, 3 December 2021 at 02:38:18 UTC, Dave P. wrote:

>

Where this really shines are with bit flags.

Bitflags are a case where 'with' does work.

More generally, I'm sceptical of the feature for the reasons Dennis and Paul mention; there is clearly some advantage wrt boilerplate in some cases, but not for bitflags.

Except that with introduces a new scope and can’t be used at file scope:

enum Flags {
    FOO = 1,
    BAR = 2,
    BAZ = 4,
    QUX = 8,
}

void main(){
    with(Flags){
        auto flags = FOO | BAR | BAZ | QUX;
    }
    // flags does not exist here.
}

with(Flags){ // Illegal
    auto f = FOO | BAR;
}

With is also very noisy syntactically when all you want is to use enums in a nicer way.

You could do this however:

    Flags flags;
    with(Flags){
        flags = FOO | BAR | BAZ | QUX;
    }
    // flags does exist here now.

Or even:

    Flags flags;
    with(Flags) flags = FOO | BAR | BAZ | QUX;
    // flags does exist here now.
December 03, 2021

On Friday, 3 December 2021 at 09:36:46 UTC, bauss wrote:

>

Or even:

    Flags flags;
    with(Flags) flags = FOO | BAR | BAZ | QUX;
    // flags does exist here now.

the proposal is to specifically avoid having to be this repetitive

Flags flags with Flags flags = ...

This is way too much cognitive load only to get to the point of simply doing FOO | BAR | BAZ | QUX

with when you think about using it, when you just want to use the enum it becomes cumbersome

December 03, 2021

On Friday, 3 December 2021 at 04:58:51 UTC, Dave P. wrote:

>

Except that with introduces a new scope

There was a proposal (with an implementation) to allow with without braces:
https://forum.dlang.org/post/bwudoaknbiodwilvldxl@forum.dlang.org

>

and can’t be used at file scope:

That should be made to work.

>

with(Flags){ // Illegal
auto f = FOO | BAR;
}


With is also very noisy syntactically when all you want is to use enums in a nicer way.

Fair point, but is there a simple solution to that without ambiguity drawbacks?

December 03, 2021

On Friday, 3 December 2021 at 05:41:20 UTC, Rumbu wrote:

>

On Thursday, 2 December 2021 at 19:44:07 UTC, russhy wrote:

>

could be implemented if we all agree that it would be a nice addition to the language!

Link: https://gist.github.com/RUSShyTwo/2fcf6d294d25ba748d61927b5eed2944

In C# you can bring all enum members into scope with using static

[...]

An equivalent D idiom will be

static alias MyEnum;

[...]

The problem with this solution (and other with based solutions) is that it is effectively undoing a nice feature of D of sane namespaces. If you use a class or a struct, you don’t have to explicitly dump all of its member functions into your current scope. You just look up the symbol in the namespace of the struct/class. Similarly, a benefit of this proposal and how it works in Jai/Zig/Odin is that the compiler already knows what type it is expecting and so you are just looking up the name in the namespace of that type. No namespace pollution and it is very convenient.

June 25, 2022

I tried to implement this, i spent the whole day today on it, unfortunately, i'm unable to make progress, to be honest, it was like diving in an endless ocean, i was clueless lol, if anyone is willing to help, that would be super cool, that feature deserve to be implemented!

Here is a test code:

--- app.d
import data;

void main()
{
  pass_enum(MyEnum.MyValueB);
  pass_enum(.MyValueA);
}

--- data.d
module data;

enum MyEnum { MyValueA, MyValueB }

void pass_enum(MyEnum e) {}

The idea is:

1- call pass_enum with .MyValueA
2- dmd should be able to know what symbol it refers to in the pass_enum function
3- dmd should check in all the known modules if he can find that symbol
4- if dmd found the symbol, check its members and compare both values
5- if it finds it, then use that symbol
6- if it doesn't find it, then fall back to the old behavior of checking in the module for values

I'm clueless about both DMD and language development, so that adds another layer of difficulty for me, trying to understand DMD code base, and overall how language and compiler works, i still have no clue how everything world.. but i still managed to get somewhere

I put an else here: https://github.com/dlang/dmd/blob/6f2a4daebabe666fbb5d9fde1aa6a0e6ddb6b70d/src/dmd/expressionsem.d#L12686

So the compiler doesn't complain about undefined identifier, this allows me to start working on step 2, and that's where i got stuck..

If anyone is willing to help me, that would be sweet

June 25, 2022

On Saturday, 25 June 2022 at 16:10:03 UTC, ryuukk_ wrote:

>

I tried to implement this, i spent the whole day today on it, unfortunately, i'm unable to make progress, [...]
If anyone is willing to help me, that would be sweet

I've implemented something similar in styx, it's called "parent enum inference" but I dont know if it can be done the same way in DMD. The architecture of the compiler is only partially similar. I'll describe how this work for me, hope this'll help ;)

So in styx this works almost like a "WithStatement":

A. Scope

(Something that can be done very similarly in DMD)

When it is wished to inferer enum parent names, I push a scope. In this scope I add as member the equivalent of a "with" expression, That exp gives, as an IdentExp, the enum.

In my case I add really the same data type as with a WithStatement, but there's a flag used to distinguish expressions used for "with" and those used for "parent enum inference". This is because in styx there is no special syntax (in opposition to the leading dot proposed for what you try to implement), it is just specified that this is tried at the very end.

Finally when it is not required to infer anymore, pop the scope.

related code:

example use, when the switched expression is an enum and to allow inference on the cases:

B. Resolving

(That part would be very different in DMD)

When an identifier has been unsuccessfully resolved, the expressions added in the scope are tried.

June 26, 2022

On Saturday, 25 June 2022 at 20:48:15 UTC, Basile B. wrote:

>

On Saturday, 25 June 2022 at 16:10:03 UTC, ryuukk_ wrote:

>

I tried to implement this, i spent the whole day today on it, unfortunately, i'm unable to make progress, [...]
If anyone is willing to help me, that would be sweet

I've implemented something similar in styx, it's called "parent enum inference" but I dont know if it can be done the same way in DMD. The architecture of the compiler is only partially similar. I'll describe how this work for me, hope this'll help ;)

So in styx this works almost like a "WithStatement":

A. Scope

(Something that can be done very similarly in DMD)

When it is wished to inferer enum parent names, I push a scope. In this scope I add as member the equivalent of a "with" expression, That exp gives, as an IdentExp, the enum.

In my case I add really the same data type as with a WithStatement, but there's a flag used to distinguish expressions used for "with" and those used for "parent enum inference". This is because in styx there is no special syntax (in opposition to the leading dot proposed for what you try to implement), it is just specified that this is tried at the very end.

Finally when it is not required to infer anymore, pop the scope.

related code:

example use, when the switched expression is an enum and to allow inference on the cases:

B. Resolving

(That part would be very different in DMD)

When an identifier has been unsuccessfully resolved, the expressions added in the scope are tried.

Thanks a lot for your detailed and helpful reply, will give it another try today!

Compiler stuff is quite intimidating

June 26, 2022

I tried.., language development is definitely not my cup of tea, i'm unable to do anything productive, hopefully someone else will be able to make use of what you have done already for Styx

Who else is willing to do it?

June 29, 2022

On Sunday, 26 June 2022 at 15:26:22 UTC, ryuukk_ wrote:

>

I tried.., language development is definitely not my cup of tea, i'm unable to do anything productive, hopefully someone else will be able to make use of what you have done already for Styx

Who else is willing to do it?

After seeing the SOAC post, i went ahead and created an issue on the github/projects https://github.com/dlang/projects/issues/88

Hopefully someone might pick it up

1 2
Next ›   Last »