Re: Subtyping of an enum

2019-04-15 Thread Alex via Digitalmars-d-learn
On Monday, 15 April 2019 at 20:36:09 UTC, Anton Fediushin wrote: On Monday, 15 April 2019 at 14:20:57 UTC, Alex wrote: On Monday, 15 April 2019 at 08:39:24 UTC, Anton Fediushin wrote: Hello! I am currently trying to add a custom `toString` method to an enum so that: 1. Enum members would still

Re: Subtyping of an enum

2019-04-15 Thread Anton Fediushin via Digitalmars-d-learn
On Monday, 15 April 2019 at 14:20:57 UTC, Alex wrote: On Monday, 15 April 2019 at 08:39:24 UTC, Anton Fediushin wrote: Hello! I am currently trying to add a custom `toString` method to an enum so that: 1. Enum members would still have numeric values and can be easily compared (things like

Re: Subtyping of an enum

2019-04-15 Thread Anton Fediushin via Digitalmars-d-learn
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

Re: Subtyping of an enum

2019-04-15 Thread Alex via Digitalmars-d-learn
On Monday, 15 April 2019 at 08:39:24 UTC, Anton Fediushin wrote: Hello! I am currently trying to add a custom `toString` method to an enum so that: 1. Enum members would still have numeric values and can be easily compared (things like `enum a { foo = "FOO", bar = "BAR”}` won't do, I want

Re: Subtyping of an enum

2019-04-15 Thread Alex via Digitalmars-d-learn
On Monday, 15 April 2019 at 08:39:24 UTC, Anton Fediushin wrote: Hello! I am currently trying to add a custom `toString` method to an enum so that: 1. Enum members would still have numeric values and can be easily compared (things like `enum a { foo = "FOO", bar = "BAR”}` won't do, I want

Re: Subtyping of an enum

2019-04-15 Thread diniz via Digitalmars-d-learn
Le 15/04/2019 à 10:39, Anton Fediushin via Digitalmars-d-learn a écrit : This seems to work just fine for assigning and comparisons but passing Enum as a function argument does not work: ``` void fun(Enum e) {} fun(Enum.foo); --- Error: function fun(Enum e) is not callable using argument types

Re: Subtyping of an enum

2019-04-15 Thread Alex via Digitalmars-d-learn
On Monday, 15 April 2019 at 13:38:33 UTC, Anton Fediushin wrote: 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 ``` This is

Re: Subtyping of an enum

2019-04-15 Thread Anton Fediushin via Digitalmars-d-learn
On Monday, 15 April 2019 at 12:25:38 UTC, XavierAP wrote: On Monday, 15 April 2019 at 10:34:42 UTC, Anton Fediushin wrote: On Monday, 15 April 2019 at 10:06:30 UTC, XavierAP wrote: [snip] Isn't this how subtyping works for integers and other types? For example, you have subtyped an integer

Re: Subtyping of an enum

2019-04-15 Thread Anton Fediushin via Digitalmars-d-learn
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;

Re: Subtyping of an enum

2019-04-15 Thread XavierAP via Digitalmars-d-learn
On Monday, 15 April 2019 at 12:38:59 UTC, XavierAP wrote: More generally you insist on modules and namespaces to be different concepts, which they are (pointlessly) for C++, but not for D (purposely). Here I should say packages instead of modules... but the general argument stays. Anyway

Re: Subtyping of an enum

2019-04-15 Thread XavierAP via Digitalmars-d-learn
On Monday, 15 April 2019 at 10:34:42 UTC, Anton Fediushin wrote: The problem here is that I want to keep methods that are related to an enum inside of this enum for purely aesthetic and organizational purposes. ... These global functions pollute global namespace. If you have defined

Re: Subtyping of an enum

2019-04-15 Thread XavierAP via Digitalmars-d-learn
On Monday, 15 April 2019 at 10:34:42 UTC, Anton Fediushin wrote: On Monday, 15 April 2019 at 10:06:30 UTC, XavierAP wrote: You have defined your sub-typing the opposite way that you wanted it to work: every `Enum` is an `internal`, but the other way around an `internal` may not work as an

Re: Subtyping of an enum

2019-04-15 Thread Alex via Digitalmars-d-learn
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: Enum.internal is private to make it inaccessible from any other place. All I want is a way to have an enum that I could extend with my own methods. Something to make the

Re: Subtyping of an enum

2019-04-15 Thread Anton Fediushin via Digitalmars-d-learn
On Monday, 15 April 2019 at 10:06:30 UTC, XavierAP wrote: On Monday, 15 April 2019 at 08:39:24 UTC, Anton Fediushin wrote: Hello! I am currently trying to add a custom `toString` method Several remarks... First of all, strings can be compared (alphabetically) as well as integers, e.g.

Re: Subtyping of an enum

2019-04-15 Thread Anton Fediushin via Digitalmars-d-learn
On Monday, 15 April 2019 at 10:00:36 UTC, Alex wrote: On Monday, 15 April 2019 at 08:39:24 UTC, Anton Fediushin wrote: [snip] Otherwise, you could alwas define fun as ´´´ void fun(Enum.internal e) {} ´´´ but I assume, you want to avoid especially this. In favor of my first proposition,

Re: Subtyping of an enum

2019-04-15 Thread XavierAP via Digitalmars-d-learn
On Monday, 15 April 2019 at 08:39:24 UTC, Anton Fediushin wrote: Hello! I am currently trying to add a custom `toString` method Several remarks... First of all, strings can be compared (alphabetically) as well as integers, e.g. assert("foo" > "bar") Perhaps not your use case, but worth

Re: Subtyping of an enum

2019-04-15 Thread Alex via Digitalmars-d-learn
On Monday, 15 April 2019 at 08:39:24 UTC, Anton Fediushin wrote: Hello! I am currently trying to add a custom `toString` method to an enum so that: 1. Enum members would still have numeric values and can be easily compared (things like `enum a { foo = "FOO", bar = "BAR”}` won't do, I want