Thread overview
opDispatch & with
Feb 17, 2023
John Chapman
Feb 17, 2023
drug007
Feb 17, 2023
drug007
Feb 17, 2023
drug007
Feb 17, 2023
John Chapman
Feb 18, 2023
drug007
Feb 17, 2023
Paul Backus
Feb 17, 2023
John Chapman
February 17, 2023

If I call opDispatch with a name that is also a type inside a with statement, I get an error.

enum Suit { clubs, spades, hearts, diamonds }

struct Card {
  void opDispatch(string s)(.Suit) {}
}

void main() {
  Card c;
  with (c) Suit = .Suit.diamonds; // Error: `Suit` is not an lvalue and cannot be modified
}

Is this a bug? It compiles without error if I call opDispatch!"Suit" directly.

February 17, 2023
17.02.2023 22:30, John Chapman пишет:
> If I call opDispatch with a name that is also a type inside a ```with``` statement, I get an error.
> 
> ```d
> enum Suit { clubs, spades, hearts, diamonds }
> 
> struct Card {
>    void opDispatch(string s)(.Suit) {}
> }
> 
> void main() {
>    Card c;
>    with (c) Suit = .Suit.diamonds; // Error: `Suit` is not an lvalue and cannot be modified
> }
> ```
> 
> Is this a bug? It compiles without error if I call opDispatch!"Suit" directly.

you are trying to assign a value `Suit.diamonds` to a type `Suit`. Could you describe what you want to do?
February 17, 2023

On Friday, 17 February 2023 at 19:30:02 UTC, John Chapman wrote:

>

If I call opDispatch with a name that is also a type inside a
[...]

Is this a bug? It compiles without error if I call opDispatch!"Suit" directly.

I think this is the same bug reported here: https://issues.dlang.org/show_bug.cgi?id=18866

February 18, 2023
17.02.2023 23:55, drug007 пишет:
> 17.02.2023 22:30, John Chapman пишет:
[snipped]

 Probably you meant something like this:
```D
import std;

enum Suit { clubs, spades, hearts, diamonds }

struct Card {
   void opDispatch(string s)(Suit t)
   {
       pragma(msg, s); // compile time output - suit
       writeln(t);     // runtime output      - diamonds
   }
}

void main() {
   Card c;
   with (c) suit = .Suit.diamonds;
}
```
February 18, 2023
18.02.2023 00:06, drug007 пишет:
> 17.02.2023 23:55, drug007 пишет:
>> 17.02.2023 22:30, John Chapman пишет:

[snipped]

I think in you initial example the compiler does not call `opDispatch` at all because it recognizes `Suit` as a type.
February 17, 2023
On Friday, 17 February 2023 at 20:55:03 UTC, drug007 wrote:
> you are trying to assign a value `Suit.diamonds` to a type `Suit`. Could you describe what you want to do?

I'm trying to assign Suit.diamonds to a property of Card named "Suit". I know properties are usually lower case, and that calling "c.Suit = Suit.diamonds" works. Just wonder why the compiler seems to overlook opDispatch here.
February 17, 2023

On Friday, 17 February 2023 at 20:56:57 UTC, Paul Backus wrote:

>

I think this is the same bug reported here: https://issues.dlang.org/show_bug.cgi?id=18866

Thanks for appending this to the bug report.

February 18, 2023
18.02.2023 00:44, John Chapman пишет:
> On Friday, 17 February 2023 at 20:55:03 UTC, drug007 wrote:
>> you are trying to assign a value `Suit.diamonds` to a type `Suit`. Could you describe what you want to do?
> 
> I'm trying to assign Suit.diamonds to a property of Card named "Suit". I know properties are usually lower case, and that calling "c.Suit = Suit.diamonds" works. Just wonder why the compiler seems to overlook opDispatch here.

My bad, sorry for noise