Jump to page: 1 2 3
Thread overview
loop through specific class members
Jan 19, 2009
Trass3r
Jan 19, 2009
Daniel Keep
Jan 19, 2009
Trass3r
Jan 19, 2009
Lutger
Jan 19, 2009
BCS
Jan 19, 2009
Trass3r
Jan 19, 2009
Daniel Keep
Jan 19, 2009
BCS
Jan 19, 2009
Daniel Keep
Jan 20, 2009
Trass3r
Jan 19, 2009
Trass3r
Jan 20, 2009
Trass3r
Jan 20, 2009
Trass3r
Jan 20, 2009
Trass3r
Jan 21, 2009
Christopher Wright
Jan 21, 2009
Trass3r
Jan 26, 2009
Sergey Gromov
Jan 26, 2009
BCS
Jan 27, 2009
Sergey Gromov
Jan 27, 2009
BCS
Mar 29, 2009
Trass3r
January 19, 2009
Is there any way to loop through specific members of a class, e.g. all public functions?
I've already seen a function getMembers in druntime's ClassInfo class but I can't find anything related to the attributes there.
January 19, 2009
Trass3r wrote:
> Is there any way to loop through specific members of a class, e.g. all
> public functions?
> I've already seen a function getMembers in druntime's ClassInfo class
> but I can't find anything related to the attributes there.

Assuming you're using D2, http://digitalmars.com/d/2.0/traits.html might prove to be of interest.

  -- Daniel
January 19, 2009
Daniel Keep schrieb:
> Assuming you're using D2, http://digitalmars.com/d/2.0/traits.html might
> prove to be of interest.
> 
>   -- Daniel

It is indeed of interest though being not exactly what I want. Seems like there's currently no way to get attributes like public etc.

But I think an acceptable workaround would be to use a common name prefix for specific methods and then use that allMembers trait.
January 19, 2009
Trass3r wrote:

> Daniel Keep schrieb:
>> Assuming you're using D2, http://digitalmars.com/d/2.0/traits.html might prove to be of interest.
>> 
>>   -- Daniel
> 
> It is indeed of interest though being not exactly what I want. Seems like there's currently no way to get attributes like public etc.
> 
> But I think an acceptable workaround would be to use a common name prefix for specific methods and then use that allMembers trait.

Indeed, perhaps it should be there. You could use getVirtualFunctions to get all public+protected member functions that are not declared final, but that's not ideal. Another possible hack: if used from a different module, you could use the 'compiles' trait with allMembers to find out if a member can be accessed.
January 19, 2009
Reply to Lutger,

> Another possible hack: if used from a
> different module, you could use the 'compiles' trait with allMembers
> to find out if a member can be accessed.
> 

you could define a template in another module that does the check and returns the result.


January 19, 2009
BCS schrieb:
> Reply to Lutger,
> 
>> Another possible hack: if used from a
>> different module, you could use the 'compiles' trait with allMembers
>> to find out if a member can be accessed.
>>
> 
> you could define a template in another module that does the check and returns the result.
> 
> 

Well, it'd indeed be used from a different module than class, in fact from a different package.
But this only gives the public members, right?
January 19, 2009

Trass3r wrote:
> BCS schrieb:
>> Reply to Lutger,
>>
>>> Another possible hack: if used from a
>>> different module, you could use the 'compiles' trait with allMembers
>>> to find out if a member can be accessed.
>>>
>>
>> you could define a template in another module that does the check and returns the result.
>>
>>
> 
> Well, it'd indeed be used from a different module than class, in fact
> from a different package.
> But this only gives the public members, right?

It depends on what exactly you're trying to do.  Some time ago, I wrote a library that created XML loaders for structs, and it needed to know the names of fields.  Pre-traits, this is what I used:

struct Stuff
{
    int foo;
    char[] bar;

    alias Tuple!("foo", "bar") _fields;
}

Then I just looped over _fields.

  -- Daniel

January 19, 2009
Reply to Daniel,


> It depends on what exactly you're trying to do.  Some time ago, I
> wrote a library that created XML loaders for structs, and it needed to
> know the names of fields.  Pre-traits, this is what I used:
> 
> struct Stuff
> {
> int foo;
> char[] bar;
> alias Tuple!("foo", "bar") _fields;
> }
> Then I just looped over _fields.
> 
> -- Daniel
> 

you can get the names even in D1.0. Not exactly clean but...

http://codepad.org/Eu16XqFu


January 19, 2009
Daniel Keep schrieb:
>>>> Another possible hack: if used from a
>>>> different module, you could use the 'compiles' trait with allMembers
>>>> to find out if a member can be accessed.
>>>>
>>> you could define a template in another module that does the check and
>>> returns the result.
>>>
>>>
>> Well, it'd indeed be used from a different module than class, in fact
>> from a different package.
>> But this only gives the public members, right?
> 
> It depends on what exactly you're trying to do.  Some time ago, I wrote
> a library that created XML loaders for structs, and it needed to know
> the names of fields.  Pre-traits, this is what I used:
> 
> struct Stuff
> {
>     int foo;
>     char[] bar;
> 
>     alias Tuple!("foo", "bar") _fields;
> }
> 
> Then I just looped over _fields.
> 

What I want is a way that doesn't require much changes to the class itself but rather does the work in the module that actually uses the method names.

What I'm trying to do is providing a mechanism for LuaLib to register a whole class automatically (and probably also checking all method's parameters for correctness). So far you have to register each method to be used in lua individually.

So I was initially thinking about registering all protected methods or something like that. Guess I will use the prefix solution here cause it probably is the best way for this particular problem anyway.

But yet I'm interested in (and also somewhat impressed by) the possibilities to do this ;)


(reminds me of my search for a way to debug template and mixin stuff)
January 19, 2009

BCS wrote:
> Reply to Daniel,
> 
> 
>> It depends on what exactly you're trying to do.  Some time ago, I wrote a library that created XML loaders for structs, and it needed to know the names of fields.  Pre-traits, this is what I used:
>>
>> struct Stuff
>> {
>> int foo;
>> char[] bar;
>> alias Tuple!("foo", "bar") _fields;
>> }
>> Then I just looped over _fields.
>>
>> -- Daniel
>>
> 
> you can get the names even in D1.0. Not exactly clean but...
> 
> http://codepad.org/Eu16XqFu

I remember trying that, but abandoning it for some reason.  This code is getting old now; over two years now, so that might not have worked back then.

  -- Daniel
« First   ‹ Prev
1 2 3