Enum Default Initializer?
Salih Dincer
salihdb at hotmail.com
Mon Dec 5 03:10:16 UTC 2022
Hi All, are these results normal?
The results have nothing to do with extern(c). I just wanted to
see the simplest results.
```d
extern (C) void main()
{
enum SAYI : char { bir = 49, iki }
enum NUMS : SAYI {
one = SAYI.bir,
two = SAYI.iki
}
import core.stdc.stdio;
printf("%c\n", SAYI.bir); // okay, ASCII: 1
printf("%c\n", NUMS.iki); // okay, ASCII: 2
printf("%c\n", NUMS.one.iki); // opps: ASCII: 2
printf("%c\n", NUMS.two.iki); // opps: ASCII: 2
// printf("%c\n", NUMS.three); // okay: ERROR
printf("%c\n", NUMS.iki); // opps: ASCII: 2
printf("%c\n", NUMS.iki.iki); // opps: ASCII: 2
enum Bar : SAYI { one }
printf("[%c]\n", Bar.one); // okay, ASCII: []
assert(Bar.one == '\0');
/* other error; Comparison between different enumeration types
`Foo` and `SAYI`; If this behavior is intended consider using
`std.conv.asOriginalType`
enum Foo : SAYI { one, two }
with (SAYI) enum Zoo : SAYI
{
one = bir, two
}
//*/
with(SAYI)
{
enum Zoo : SAYI
{
one = bir,
two = iki,
three = cast(SAYI)'3'
}
printf("%c\n", Zoo.three); // okay, ASCII: 3
}
}
```
[v2.099.0 Specification on the
subject:](https://docarchives.dlang.io/v2.099.0/spec/enum.html)
> 7. If there is no AssignExpression and it is not the first
> EnumMember, it is given the value of the previous EnumMember+1.
> If the value of the previous EnumMember is EnumBaseType.max, it
> is an error. If the value of the previous EnumMember+1 is the
> same as the value of the previous EnumMember, it is an error.
> (This can happen with floating point types.)
>
> 8. All EnumMembers are in scope for the AssignExpressions.
> ```d
> enum C
> {
> A = B, // A = 4
> B = D, // B = 4
> C = 3, // C = 3
> D // D = 4
> }
> enum E : C
> {
> E1 = C.D,
> E2 // error, C.D is C.max
> }
> ```
SDB at 79
More information about the Digitalmars-d-learn
mailing list