Idiomatic way to call base method in generic code
ZombineDev via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon May 25 02:24:56 PDT 2015
On Monday, 25 May 2015 at 07:57:49 UTC, ketmar wrote:
> i don't know why you want that, but something like this may do:
>
> auto callBaseMethod(string MTN, C, Args...) (inout C self, Args
> args) {
> alias FSC = BaseClassesTuple!(C)[0];
> return mixin(`self.`~FSC.stringof~`.`~MTN~"(args)");
> }
>
> writeln(d.callBaseMethod!"toString");
>
Thanks!
[1]:
https://github.com/ZombineDev/Chess2RT/blob/c36ba3e73744cf3912c25abccedbbd742f7f5be3/source/util/prettyprint.d#L7
[2]:
https://github.com/ZombineDev/Chess2RT/blob/master/source/util/prettyprint.d#L14
I initially had [1] a string mixin which goes through all the
members of a class and prints them and also prints its base
class' members, but then it seemed that it would be more cleaner
if I could use a function template [2] (which also improved my
debug experience) so I started exploring alias, but they turned
out to be less powerful then I thought:
alias can't refer to a nested member:
-------------
struct Point2
{
float x;
float y;
}
struct Line2
{
Point2 start;
Point2 end;
mixin Access;
// alias x1 = this.start.x; <- this doesn't work :(
alias x1 = get!"start.x";
alias y1 = get!"start.y";
alias x2 = get!"end.x";
alias y2 = get!"end.y";
}
// I need to use a mixin to flatten the access :(
private mixin template Access()
{
ref auto get(string accessPattern)() inout
{
return mixin(accessPattern);
}
}
-------------
So I wondered if I'm missing something and decided to ask here.
> no. and again, why do you want that? why do you people are so
> afraid of
> string mixins? ;-)
Well I'm not afraid of string mixins, but the `alias` keyword
seemed to possess some mysterious power (I was also under the
influence of this [3] blog post) which I wanted to explore. Also
they're a bit more elegant.
[3]:
http://blog.thecybershadow.net/2015/04/28/the-amazing-template-that-does-nothing/
More information about the Digitalmars-d-learn
mailing list