www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - manually call opUnary

reply "Algo" <qq qq.qq> writes:
Is it possible?
As in
{
      int a;
      a.opUnary!"++"();
}
no property 'opUnary' for type 'int'
Nov 03 2014
next sibling parent "Algo" <qq qq.qq> writes:
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
Nov 03 2014
prev sibling next sibling parent reply Rikki Cattermole <alphaglosined gmail.com> writes:
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: ++ --
Nov 03 2014
parent "Algo" <qq qq.qq> writes:
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.
Nov 03 2014
prev sibling parent Jonathan M Davis via Digitalmars-d-learn writes:
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
Nov 04 2014