enum confusion

Salih Dincer salihdb at hotmail.com
Tue May 10 19:58:53 UTC 2022


On Monday, 9 May 2022 at 01:50:44 UTC, Don Allen wrote:
> [...]
> Foo.bar is *not* a manifest constant according to the Language 
> Ref; it's a Named Enum as described in 17.1. 17.3 defines 
> manifest constants as a special case of Anonymous Enums, those 
> having a single member. But if you add another member to the 
> Named Enum in my test:
>
> ````
>   1 import std.stdio;
>   2
>   3 enum Foo {
>   4     bar,
>   5     baz
>   6 }
>   7
>   8 int main(string[] args)
>   9 {
>  10     writefln("debug: %d\n", &(Foo.bar));
>  11     return 0;
>  12 }
> ````
>
> making a Named Enum with multiple members -- certainly not what 
> the documentation is calling a manifest constant -- and yet you 
> get the same error message from the compiler, referring to 
> 'bar' as a manifest constant.

Parentheses are there to make things easier for me.  Same for the 
compiler.  That is to avoid confusion or misunderstanding.  We 
get the same result in the 8 examples below.

```d
struct Foo {
   int bar;
   alias bar this;
   auto far() { return &bar; }
}
void main()
{
   Foo foo;
   // to direct
   "1: ".writeln(&foo.bar);
   (&foo.bar).writefln!"2. %s";
   "3: ".writeln(&(foo.bar));
   (&(foo.bar)).writefln!"4. %s";
   // by alias
   "5. ".writeln(&foo);
   (&foo).writefln!"6. %s";
   // by function
   "7: ".writeln(&(*foo.far));
   (&(*foo.far)).writefln!"8. %s";
}
```
We do similar things for a mathematical operation, or we avoid 
parentheses if they comply with arithmetic precedence rules.

In summary, for us humans, sometimes the definitions are 
different, but in machine language everything is the same.

SDB at 79


More information about the Digitalmars-d mailing list