digitalmars.D.learn - foreach of classes
- Lucien (20/20) Apr 09 2016 Hello.
- Basile B. (2/22) Apr 09 2016 remove auto and optionally replaces it with TypeInfo_Class.
- Lucien (3/29) Apr 09 2016 So simple ?! o.o
- Nicholas Wilson (5/35) Apr 09 2016 FYI the things that you can put there (in place of auto) are
- Basile B. (4/12) Apr 09 2016 Yes, it's right that to put the type is a noop, but otherwise DCD
- Steven Schveighoffer (11/30) Apr 12 2016 I know this is not exactly what you may be expecting, but you could do
Hello. When I do: --------------------- class MyClass{..} class YourClass{..} class OurClass{..} YourClass yc = new YourClass(); foreach (auto id; [ typeid(MyClass), typeid(YourClass), typeid(OurClass) ]) { if (typeid(yc) == id) { writeln("It works !"); } } --------------------- The compiler says: basic type expected, not auto Why can't I have one time the type id of MyClass, then of YourClass and of OurClass ? Is there an alternative ?
Apr 09 2016
On Saturday, 9 April 2016 at 10:10:19 UTC, Lucien wrote:Hello. When I do: --------------------- class MyClass{..} class YourClass{..} class OurClass{..} YourClass yc = new YourClass(); foreach (auto id; [ typeid(MyClass), typeid(YourClass), typeid(OurClass) ]) { if (typeid(yc) == id) { writeln("It works !"); } } --------------------- The compiler says: basic type expected, not auto Why can't I have one time the type id of MyClass, then of YourClass and of OurClass ? Is there an alternative ?remove auto and optionally replaces it with TypeInfo_Class.
Apr 09 2016
On Saturday, 9 April 2016 at 10:28:05 UTC, Basile B. wrote:On Saturday, 9 April 2016 at 10:10:19 UTC, Lucien wrote:So simple ?! o.o Thanks !Hello. When I do: --------------------- class MyClass{..} class YourClass{..} class OurClass{..} YourClass yc = new YourClass(); foreach (auto id; [ typeid(MyClass), typeid(YourClass), typeid(OurClass) ]) { if (typeid(yc) == id) { writeln("It works !"); } } --------------------- The compiler says: basic type expected, not auto Why can't I have one time the type id of MyClass, then of YourClass and of OurClass ? Is there an alternative ?remove auto and optionally replaces it with TypeInfo_Class.
Apr 09 2016
On Saturday, 9 April 2016 at 10:56:34 UTC, Lucien wrote:On Saturday, 9 April 2016 at 10:28:05 UTC, Basile B. wrote:FYI the things that you can put there (in place of auto) are nothing at all (type is inferred, a type, ref (the foreach variable is taken by reference), const/immutable (the variable is const or immutable).On Saturday, 9 April 2016 at 10:10:19 UTC, Lucien wrote:So simple ?! o.o Thanks !Hello. When I do: --------------------- class MyClass{..} class YourClass{..} class OurClass{..} YourClass yc = new YourClass(); foreach (auto id; [ typeid(MyClass), typeid(YourClass), typeid(OurClass) ]) { if (typeid(yc) == id) { writeln("It works !"); } } --------------------- The compiler says: basic type expected, not auto Why can't I have one time the type id of MyClass, then of YourClass and of OurClass ? Is there an alternative ?remove auto and optionally replaces it with TypeInfo_Class.
Apr 09 2016
On Saturday, 9 April 2016 at 11:03:54 UTC, Nicholas Wilson wrote:On Saturday, 9 April 2016 at 10:56:34 UTC, Lucien wrote:Yes, it's right that to put the type is a noop, but otherwise DCD doesn't work in foreach. Personally I think it s*c*s a lot. The manual specification of the type should be allowed.On Saturday, 9 April 2016 at 10:28:05 UTC, Basile B. wrote:FYI the things that you can put there (in place of auto) are nothing at all (type is inferred, a type, ref (the foreach variable is taken by reference), const/immutable (the variable is const or immutable).On Saturday, 9 April 2016 at 10:10:19 UTC, Lucien wrote:Hello.
Apr 09 2016
On 4/9/16 6:10 AM, Lucien wrote:Hello. When I do: --------------------- class MyClass{..} class YourClass{..} class OurClass{..} YourClass yc = new YourClass(); foreach (auto id; [ typeid(MyClass), typeid(YourClass), typeid(OurClass) ]) { if (typeid(yc) == id) { writeln("It works !"); } } --------------------- The compiler says: basic type expected, not auto Why can't I have one time the type id of MyClass, then of YourClass and of OurClass ? Is there an alternative ?I know this is not exactly what you may be expecting, but you could do what you want this way: import std.meta: AliasSeq; foreach(classType; AliasSeq!(MyClass, YourClass, OurClass)) { // in here, classType is now an alias for the actual class auto id = typeid(classType); // if you want it in TypeInfo_Class form } This is called a static foreach. -Steve
Apr 12 2016