www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - opDispatch & with

reply John Chapman <johnch_atms hotmail.com> writes:
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.
Feb 17 2023
next sibling parent reply drug007 <drug2004 bk.ru> writes:
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?
Feb 17 2023
next sibling parent reply drug007 <drug2004 bk.ru> writes:
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; } ```
Feb 17 2023
parent drug007 <drug2004 bk.ru> writes:
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.
Feb 17 2023
prev sibling parent reply John Chapman <johnch_atms hotmail.com> writes:
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.
Feb 17 2023
parent drug007 <drug2004 bk.ru> writes:
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
Feb 18 2023
prev sibling parent reply Paul Backus <snarwin gmail.com> writes:
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
Feb 17 2023
parent John Chapman <johnch_atms hotmail.com> writes:
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.
Feb 17 2023