Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enum siblings and struct member naming conflict undetected and counter intuitive path prefered #3998

Open
dlangBugzillaToGithub opened this issue Jun 9, 2023 · 1 comment

Comments

@dlangBugzillaToGithub
Copy link

crazymonkyyy reported this on 2023-06-09T17:45:54Z

Transferred from https://issues.dlang.org/show_bug.cgi?id=23981

CC List

  • Dennis

Description

```d
import std;
struct button_{
	int j;
	bool opCast(T:bool)(){
		"casted".writeln;
		return up;//down;
	}
	bool down(){
		assert(j==0,"called down on something not shift "~j.to!string);
		return true;
	}
	bool up(){
		assert(0,"I thought I was calling down");
	}
}
enum button{
	shift=button_(0),down=button_(1)
}
unittest{
	if(button.shift.down){
		"shift was pressed".writeln;
	}
}
enum abc{a,b,c}
unittest{
	abc foo;
	foo=foo.a.b.c.c.b.a;//?????????
}
```

I expect the enum button to contain a reference of shift that I can then ask if its down

Instead, the shift has a sibling called down and that is the interpretation the compiler prefers; this needs a warning or an error
@dlangBugzillaToGithub
Copy link
Author

dkorpel commented on 2023-06-09T19:45:28Z

I reduced the example a bit:

```D
struct B
{
    int j;
    bool down() {return true;}
}

enum button
{
    shift = B(0),
    down = B(1), // without this line, it compiles
}

void main()
{
    // Error: struct `B` does not overload ()
    if (button.shift.down()) {}
}
```

This is indeed very confusing, and a result of D allowing you to access static properties of a type using an instance of that type. I don't know if the specification says one of them is supposed to have priority.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant