Stop using the enum as a manifest constant.

Mathias LANG geod24 at gmail.com
Sun Jun 6 13:21:35 UTC 2021


On Sunday, 6 June 2021 at 11:16:29 UTC, Jack Applegame wrote:
> On Sunday, 6 June 2021 at 10:47:54 UTC, Mathias LANG wrote:
>> I wouldn't go that far. `enum` are like `#define`, and there 
>> *are* cases where you want them. E.g. when using them as 
>> define arguments:
>> ```D
>> void foo (char[] fmt = DEFAULT_VALUE) { /* ... */ }
>> ```
>>
>> If `DEFAULT_VALUE` is `immutable`, this will never compile, 
>> but with `enum`, it might.
>
> Really?
> ```d
> enum DEFAULT_VALUE = "aaaa";
> void foo (char[] fmt = DEFAULT_VALUE) {} // Error: cannot 
> implicitly convert expression `"aaaa"` of type `string` to 
> `char[]`
> ```

It *might*, e.g. the following:
```D
enum DEFAULT = foo();
char[] foo () { return null; }
void bar (char[] arg = DEFAULT);
```


More information about the Digitalmars-d mailing list