Thread overview
manually call opUnary
Nov 04, 2014
Algo
Nov 04, 2014
Algo
Nov 04, 2014
Rikki Cattermole
Nov 04, 2014
Algo
Nov 04, 2014
Jonathan M Davis
November 04, 2014
Is it possible?
As in
{
     int a;
     a.opUnary!"++"();
}
no property 'opUnary' for type 'int'
November 04, 2014
On Tuesday, 4 November 2014 at 07:19:05 UTC, Algo wrote:
> Is it possible?
> As in
> {
>      int a;
>      a.opUnary!"++"();
> }
> no property 'opUnary' for type 'int'

((ref typeof(a) x) => ++x)(a);
works
November 04, 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:
++
--
November 04, 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.
November 04, 2014
On Tuesday, November 04, 2014 07:19:03 Algo via Digitalmars-d-learn wrote:
> Is it possible?
> As in
> {
>       int a;
>       a.opUnary!"++"();
> }
> no property 'opUnary' for type 'int'

opUnary only exists when it's been declared on a user-defined type. The way to use it generically is to use the actual operator - ++ in this case. There might be a case where calling opUnary directly makes sense, but in general, it really doesn't. Regardless, it doesn't exist for built-ins.

- Jonathan M Davis