Re: Transparent cast from class to member pointer?

2019-04-15 Thread Robert M. Münch via Digitalmars-d-learn
On 2019-04-14 20:01:27 +, diniz said: Le 14/04/2019 à 20:03, Robert M. Münch via Digitalmars-d-learn a écrit : struct IM; struct C {  IM *impl; }; int cInit(C* self); class I { C handler; this(){cInit();} } Is there a simple way that I can use handler without the address-of

Subtyping of an enum

2019-04-15 Thread Anton Fediushin via Digitalmars-d-learn
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 `a.foo < a.bar)`) 2. More custom methods can be implemented in the

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 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: I really don't understand DUB

2019-04-15 Thread Paolo Invernizzi via Digitalmars-d-learn
On Sunday, 14 April 2019 at 20:51:10 UTC, Andre Pany wrote: On Sunday, 14 April 2019 at 19:46:41 UTC, Ron Tarrant wrote: [...] You are totally right, it should be more intuitive how to use dub. As far as I know if you do not specify in dub.json/dub.sdl what type of package you have

Re: Transparent cast from class to member pointer?

2019-04-15 Thread Ali Çehreli via Digitalmars-d-learn
On 04/14/2019 11:03 AM, Robert M. Münch wrote: struct IM; struct C {  IM *impl; }; int cInit(C* self); class I { C handler; this(){cInit();} } Is there a simple way that I can use handler without the address-of operator and automatically get *impl? Something like: class I {   

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 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: I really don't understand DUB

2019-04-15 Thread Russel Winder via Digitalmars-d-learn
On Sun, 2019-04-14 at 20:51 +, Andre Pany via Digitalmars-d-learn wrote: […] > As far as I know if you do not specify in dub.json/dub.sdl what > type of package you have (executable/library) dub make a guess. > Is there an app.d it will default the targetType "executable", if > not it will

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 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: Transparent cast from class to member pointer?

2019-04-15 Thread Alex via Digitalmars-d-learn
On Monday, 15 April 2019 at 15:07:10 UTC, Robert M. Münch wrote: On 2019-04-15 08:19:57 +, Ali ‡ehreli Bingo, I didn't know that I can do an 'alias this' using a function and not only a type... pretty cool. So, with several of these I can setup implicit conversions to different types.

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 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 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: Transparent cast from class to member pointer?

2019-04-15 Thread diniz via Digitalmars-d-learn
Le 15/04/2019 à 08:30, Robert M. Münch via Digitalmars-d-learn a écrit : The C side requires that *impl is the 1st member in the struct/class whereever it is stored. Hence, the wrapping in a struct and not directly putting it into a D class. All right! Did not think at this usage case,

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: Transparent cast from class to member pointer?

2019-04-15 Thread Robert M. Münch via Digitalmars-d-learn
On 2019-04-15 08:19:57 +, Ali ‡ehreli said: 'alias this' can do that: Hi, I had the suspicion already... struct IM; struct C { IM *impl; }; int cInit(C* self) { return 0; } class I { C handler; this(){cInit();} C* ptr() { // <== ADDED return

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 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: I really don't understand DUB

2019-04-15 Thread diniz via Digitalmars-d-learn
Le 15/04/2019 à 09:57, Paolo Invernizzi via Digitalmars-d-learn a écrit : I guess that just killing all that educated dub guess will turn dub into a much easier tool to grasp. Probably. I had similar issues as well. Now I keep all as simple and explicit as possible. (And it works, at least in

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 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: I really don't understand DUB

2019-04-15 Thread Ron Tarrant via Digitalmars-d-learn
On Sunday, 14 April 2019 at 20:51:10 UTC, Andre Pany wrote: To solve your specify the targetType explicitly in your dub.sdl file. https://dub.pm/package-format-json.html#target-types Kind regards Andre Thanks, Andre. Yeah, I did sort that out. My dilemma ATM is why I'm (seemingly

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 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: Finding Super class from Derived Class at compile time

2019-04-15 Thread Adam D. Ruppe via Digitalmars-d-learn
On Monday, 15 April 2019 at 05:20:37 UTC, Ali Çehreli wrote: BaseClassesTuple and friends: https://dlang.org/phobos/std_traits.html And the implementation of that is the `is` expression https://dlang.org/spec/expression.html#IsExpression specifically, with the `super` keyword. static if

Re: Finding Super class from Derived Class at compile time

2019-04-15 Thread LeqxLeqx via Digitalmars-d-learn
On Monday, 15 April 2019 at 22:45:59 UTC, Adam D. Ruppe wrote: On Monday, 15 April 2019 at 05:20:37 UTC, Ali Çehreli wrote: BaseClassesTuple and friends: https://dlang.org/phobos/std_traits.html And the implementation of that is the `is` expression