Members as first class citizens!

Patience via Digitalmars-d digitalmars-d at puremagic.com
Sat Feb 27 10:48:27 PST 2016


Ok, maybe not but this is what I mean:

Why can't we pass member as as sort of "objects" in there own 
right to be used for accessing objects?

e.g.,

class A
{
    int? foo;
    A Parent;

    T GetAncestorValue(member<T> field) // member is a new keyword
    {
        var p = this;
        while (!p && !p.field.HasValue)
        {
            p = p.Parent;
        }
        return p.field.Value;
    }
}

(This is pseudo D/C# code)


Then

auto x = a.GetAncestorValue(A:foo) would the properly initialized 
x.

The code is simple, logical, and makes sense(since foo is just an 
"offset" and a type. It has type safety and doesn't resort to 
reflection and passing members as strings, etc. It allows for 
general access of members rather than having to jump through a 
bunch of hoops. It should be much faster too.

Is there any fundamental theoretical reason why such a semantic 
could not be implemented in current object oriented compilers?




More information about the Digitalmars-d mailing list