manually call opUnary
Rikki Cattermole via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Nov 3 23:49:09 PST 2014
On 4/11/2014 8:19 p.m., Algo wrote:
> Is it possible?
> As in
> {
> int a;
> a.opUnary!"++"();
> }
> no property 'opUnary' for type 'int'
For primitives it doesn't look like it.
To confirm this, we'll first figure out what TypeInfo is used for it:
pragma(msg, typeid(int).name);
/d133/f260.d(16): Error: no property 'name' for type 'object.TypeInfo'
/d133/f260.d(16): while evaluating pragma(msg, (&typeid(int)).name)
So straight object.TypeInfo. Here is it in druntime:
https://github.com/D-Programming-Language/druntime/blob/master/src/object.di#L66
Because of this, it means its most definitely handled by the compiler
itself.
But you can freely do this for classes:
import std.stdio;
class Foo {
void opUnary(string op)() {
writeln(op);
}
}
void main() {
Foo foo = new Foo;
foo++;
foo.opUnary!"--";
}
Outputs:
++
--
More information about the Digitalmars-d-learn
mailing list