Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
April 09, 2016 foreach of classes | ||||
---|---|---|---|---|
| ||||
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 ? |
April 09, 2016 Re: foreach of classes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lucien | 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.
|
April 09, 2016 Re: foreach of classes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Basile B. | 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 !
|
April 09, 2016 Re: foreach of classes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lucien | 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).
|
April 09, 2016 Re: foreach of classes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nicholas Wilson | 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.
|
April 12, 2016 Re: foreach of classes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lucien | 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
|
Copyright © 1999-2021 by the D Language Foundation