Tools for building a dynamic derived type? I need a push in the right direction.
Kapps
opantm2+spam at gmail.com
Tue Aug 20 20:37:42 PDT 2013
On Tuesday, 20 August 2013 at 18:55:44 UTC, Gary Willoughby wrote:
> 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.
AutoImplement
(http://dlang.org/phobos/std_typecons.html#.AutoImplement) from
std.typecons should do what you want. Instead of implementing
only abstract methods, you could make it implement all non-final
methods by changing the 'what' template parameter to simply
return true (final methods are skipped regardless). Then you
could change the 'how' template parameter to return
ReturnType!parent.init unless an argument is set in an AA. I
can't say I've ever tried this though, so I'm not sure how well
it would work in reality.
https://github.com/QAston/DMocks-revived may interest you as well.
More information about the Digitalmars-d-learn
mailing list