www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - foreach of classes

reply Lucien <lucien.perregaux gmail.com> writes:
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
next sibling parent reply Basile B. <b2.temp gmx.com> writes:
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
parent reply Lucien <lucien.perregaux gmail.com> writes:
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:
 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.
So simple ?! o.o Thanks !
Apr 09 2016
parent reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
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:
 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.
So simple ?! o.o Thanks !
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).
Apr 09 2016
parent Basile B. <b2.temp gmx.com> writes:
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:
 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:
 Hello.
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).
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.
Apr 09 2016
prev sibling parent Steven Schveighoffer <schveiguy yahoo.com> writes:
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