Subtyping of an enum

Anton Fediushin fediushin.anton at yandex.com
Mon Apr 15 20:13:59 UTC 2019


On Monday, 15 April 2019 at 14:11:05 UTC, diniz wrote:
> Le 15/04/2019 à 10:39, Anton Fediushin via Digitalmars-d-learn 
> a écrit :
>> [snip]
>
> I don't understand why you just don't call fun with an Enum 
> (struct) param, since that is how fun is defined. This works by 
> me (see call in main):
>
> struct Enum {
>   private {
>     enum internal {
>       foo,
>       bar
>     }
>     internal m_enum;
>   }
>   this (internal i) { m_enum = i; }
>   alias m_enum this;
>   string toString() {
>     switch (this.m_enum) {
>         case internal.foo : return "FOO" ;
>         case internal.bar : return "BAR" ;
>         default : assert(0) ;
>     }
>   }
> }
>
> void fun (Enum e) {
>     writeln(e) ;
> }
>
> void main() {
>     auto e = Enum(Enum.foo) ;
>     fun(e) ;	// -> "FOO"
> }
>
> [And I wouldn't make the enum (little e) private, this just 
> risks complicating code, also eg in testing, I would just not 
> export it.]

`fun(Enum(Enum.foo));` would obviously work but `fun(Enum.foo);` 
would not. `Enum(Enum` is just redundant, I was looking for a 
solution that would make code cleaner.

I don't see a problem marking internal enum as private because it 
isn't accessed outside of the struct



More information about the Digitalmars-d-learn mailing list