More Intuitive Syntax for Manifest (enum) Constants

Peter C peterc at gmail.com
Sat Nov 22 08:10:44 UTC 2025


On Saturday, 22 November 2025 at 05:05:31 UTC, Paul Backus wrote:
> On Saturday, 22 November 2025 at 02:30:12 UTC, Peter C wrote:
>> On Saturday, 22 November 2025 at 01:48:43 UTC, Walter Bright 
>> wrote:
>>> ```d
>>> enum N = 42;
>>> ```
>>
>> I've never ever liked this misuse of enum in D to basically 
>> reflect a lightweight constant.
>
> This is actually mostly inherited from C.
>
> In C, you can write code like this to define a compile-time 
> constant:
>
>     enum { N = 42 };
>
> The only difference in D is that you are allowed to leave out 
> the curly braces.

In my opinion, there is far too much overloading of keywords in 
programming languages (both within, and across). It's always 
creating mental dissonance.

When I see [enum N = 42;] it immediately creates mental 
dissonance (perhaps because I don't see it often enough, 
thankfully).

With this, I have to pause for a moment to remember that it's 
actually attempting to define a constant named N without defining 
the containing enum type.

I guess because of D's close ties to C, the C 'enum trick' makes 
sense to have in D also. But even so....

In C#, we'd just do: [ public const int N = 42; ] - It provides 
immediate unambiguous clarity. I do not have to pause and make 
sense of this. It's a constant integer expression.

An enum type in C# is something separate altogether - a named 
type definition, for a set of related members.

I guess I can live with enum N = 42; .. but still....


More information about the Digitalmars-d mailing list