Subtyping of an enum

Anton Fediushin fediushin.anton at yandex.com
Mon Apr 15 13:38:33 UTC 2019


On Monday, 15 April 2019 at 10:45:26 UTC, Alex wrote:
> On Monday, 15 April 2019 at 10:15:50 UTC, Anton Fediushin wrote:
>> On Monday, 15 April 2019 at 10:00:36 UTC, Alex wrote:
>> [snip]
>
> This would:
>
> ´´´
>
> struct Enum {
>   private {
>     enum internal {
>       foo,
>       bar
>     }
>     internal m_enum;
>   }
>   this(internal i) { m_enum = i; }
>   alias m_enum this;
>   string toString() {
>     if(m_enum == internal.foo)
>         return "FOO";
>     else
>         return "BAR";
>   }
> }
>
> void fun(Enum e) {}
>
>
> void main(){
>     import std.stdio;
>     fun(Enum.init);
>     Enum a = Enum.foo;
>     Enum b = Enum.bar;
>     assert(a == Enum.foo);
>     assert(a < b);
>     assert(a.toString == "FOO");
>     assert(b.toString == "BAR");
>     writeln(a); // FOO
>     writeln(b); // BAR
> }
> ´´´
>
> Assuming, that automatic generation of "FOO" from foo was not 
> part of your question :-p

This does work unless I want to use it like this:
```
fun(Enum.foo);
---
Error: function fun(Enum e) is not callable using argument types 
(internal)
cannot pass argument foo of type internal to parameter Enum e
```





More information about the Digitalmars-d-learn mailing list