Tools for building a dynamic derived type? I need a push in the right direction.
Gary Willoughby
dev at nomad.so
Tue Aug 20 11:55:42 PDT 2013
I've been researching ways to accomplish creating a template that
creates a special kind of derived type and i think i need a push
in the right direction. Take this simple class:
class Person
{
private string _name;
private int _age;
this(string name, int age)
{
this._name = name;
this._age = age;
}
public string getName()
{
return this._name;
}
public int getAge()
{
return this._age;
}
}
I want to create a mock of this class which would mean achieving
the following:
1). Extend the class
2). Override all public methods to do nothing (or look for
enabled methods)
3). Provide a way of dynamically enabling methods with new return
values
Extending the class is pretty easy and adding methods dynamically
looks to be solved (TDPL 12.11.1) but it's the override all
public methods to do something different which is beating me. I
know it will probably involve mixins and dealing with tuples
somewhere along the line but documentation is lacking everywhere
i look.
std.traits gives me information about a type but using that
information is where i need the documentation. e.g. i know the
traits methods return tuples but how do you work with these
tuples? how to Iterate through them? I seem to always get stuck
when trying to iterate through tuples and generate compiler
errors.
I've taken a look at the std.typecons source especially BlackHole
and WhiteHole templates but i get lost. They are almost doing
what i want but for abstract methods.
The general usage is probably going to be something like this:
class Mock(T) : T
{
// Implementation
}
auto cat = Mock!Cat("Ginger"); // constructors supported.
auto person = Mock!Person();
person.addMethod("getName", delegate(){
return "Foo";
});
person.addMethod("getAge", delegate(){
return 10;
});
Any help will be greatly appreciated.
More information about the Digitalmars-d-learn
mailing list