www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Should have property

reply Salih Dincer <salihdb hotmail.com> writes:
In the code snippet below should have  property. Otherwise, we 
get the result this:

 40
 NP!(Foo!int)(opps!)
 NP!(Foo!int)(opps!)
```d struct NP(V) { private V payload; this(V x) { payload = x; } alias opCall this;   property: // <-- should be here  auto opCall() inout // ditto { return payload.value; } auto opCall(T)(T x) { return payload.value = x; } } class Foo(T) { private T value; NP!Foo wrapper; this() { wrapper = NP!Foo(this); } override string toString() { return "opps!"; } } import std.stdio; void main() { auto foo = new Foo!int; foo.value = 40; foo.value.writeln;   // 40 foo.wrapper.writeln; // 40 foo.wrapper = 41; foo.wrapper.writeln; // 41 } ``` SDB 79
Jan 15 2023
parent reply drug007 <drug2004 bk.ru> writes:
15.01.2023 18:54, Salih Dincer пишет:
 In the code snippet below should have  property. Otherwise, we get the 
 result this:
 
 40
 NP!(Foo!int)(opps!)
 NP!(Foo!int)(opps!)
```d struct NP(V) {   private V payload;   this(V x)   {     payload = x;   }   alias opCall this;    property: // <-- should be here   auto opCall() inout // ditto   {     return payload.value;   }   auto opCall(T)(T x)   {     return payload.value = x;   } } class Foo(T) {   private T value;   NP!Foo wrapper;   this()   {     wrapper = NP!Foo(this);   }   override string toString()   {     return "opps!";   } } import std.stdio; void main() {   auto foo = new Foo!int;   foo.value = 40;   foo.value.writeln;   // 40   foo.wrapper.writeln; // 40   foo.wrapper = 41;   foo.wrapper.writeln; // 41 } ``` SDB 79
Yes, this is why I use property despite advises to not use it. property attribute makes your method be treated like a field, not as a method by returning the method calling result, not the method itself. So in you code if you use property you get value, if you do not use it you get method. Another way is you can add handling for methods and return the method calling result yourself. But it's impossible in this case because it is handled by Phobos.
Jan 16 2023
parent Salih Dincer <salihdb hotmail.com> writes:
On Monday, 16 January 2023 at 10:37:08 UTC, drug007 wrote:
 ...
  property attribute makes your method be treated like a field, 
 not as a method by returning the method calling result, not the 
 method itself
 ...
There is also such a situation, but the last version was fixed: ```d void main() { struct Index(T) { T value; this(T v) { value = v; }; alias opCall this; property opCall() inout { return value; } } enum one= Index!int(1)(); int[one] a = 1; int[Index!int(2)()] b = 2; static assert(a.length < b.length); } ``` SDB 79
Jan 16 2023