manually call opUnary
Algo via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Nov 3 23:54:22 PST 2014
On Tuesday, 4 November 2014 at 07:49:15 UTC, Rikki Cattermole
wrote:
> 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:
> ++
> --
Nice investigation, thanks!
In practice this isn't an issue, i'll just use the lambda
expression previously posted.
More information about the Digitalmars-d-learn
mailing list