Is it possible to store properties via opDispatch using tuples?
Gary Willoughby
dev at nomad.so
Thu Jan 16 12:26:57 PST 2014
What i would like to achieve is to dynamically assign and
retrieve properties without declaring them first. For example:
class T
{
public this()
{
this.foo = "bar";
}
}
Ordinarily the above won't compile because 'foo' hasn't been
declared but with opDispatch i can handle this. The problem is
how do i handle different types of each property.
I was thinking about something like this:
class A
{
}
class B : A
{
}
class C : B
{
}
class T
{
private Tuple[string] _properties;
public this()
{
this.a = new A();
this.b = new B();
this.c = new C();
}
public void opDispatch(string name, T)(T element)
{
this._properties[name] = Tuple(T, element);
}
public auto opDispatch(string name)()
{
if (name in this._properties)
{
return
cast(this._properties[name][0])this._properties[name][1];
}
}
}
Of course this doesn't compile but is this actually possible?
i.e. storing the type and data. Then on retrieval returning the
correct data cast to the correct type? All done dynamically
without any property being pre-declared.
More information about the Digitalmars-d-learn
mailing list