Is 'alias this' too buggy/limited to be usable?
Dennis
dkorpel at gmail.com
Thu Jan 4 17:36:07 UTC 2018
I was trying to extend a type using alias this:
```
struct Number {
double value;
alias value this;
//possible extra functionality
}
import std.math: cos, abs, pow;
void main() {
auto num = Number(3);
auto a = cos(num);
auto b = abs(num);
auto c = pow(2, num);
}
```
'a' works: cos has an overload of double and because of alias
this the 'value' field of Number is passed.
'b' doesn't work: it resolves the generic type of abs to Number
and tries to convert the returned double value to a 'Number',
instead of resolving the type to double.
'c' doesn't work because the constraint isFloatingPoint!Number is
not met.
Apparently the `alias this` is not considered during type
inference of templates, while according to the Liskov
substitution principle they should be accepted.
So I searched around and found a bunch of related (unfixed as of
yet) issues:
https://issues.dlang.org/show_bug.cgi?id=5363 (const + alias this
= wrong code)
https://issues.dlang.org/show_bug.cgi?id=14499 (segfault on alias
this to self)
https://issues.dlang.org/show_bug.cgi?id=13189 (`alias this` is
not transitive)
https://issues.dlang.org/show_bug.cgi?id=5380 (alias this is not
considered with superclass lookup)
All have to do with alias this not being considered when you want
to, or being considered when you don't want it to. 'alias this'
sounds like a really cool feature to me, but all these
limitations scare me off.
Is it usable at the moment? Any improvements planned? I'd like to
hear some opionions or possible workarounds.
More information about the Digitalmars-d
mailing list