digitalmars.D.learn - Calls `this(this)` extra with delegate stuff in the code
- Joel (40/40) Jan 21 2017 Compile this and see, (it's crazy!):
- =?UTF-8?Q?Ali_=c3=87ehreli?= (21/21) Jan 21 2017 Simplified:
- =?UTF-8?Q?Ali_=c3=87ehreli?= (7/8) Jan 21 2017 TIL! Change the signature and it works without copies:
- =?UTF-8?Q?Ali_=c3=87ehreli?= (3/7) Jan 21 2017 Ugh... :( It's not a post-blit. Then what is it?
- Basile B. (18/26) Jan 21 2017 This is a __ctor that takes another instance as param. The param
- =?UTF-8?Q?Ali_=c3=87ehreli?= (20/48) Jan 21 2017 Wow! Thanks.
- Basile B. (3/26) Jan 21 2017 Same mood. I'm quite astonished to see that something like that
Compile this and see, (it's crazy!): import std.stdio; struct Widget { private int[] array; this(uint length) { array = new int[length]; } this(this) { writeln( "this(this) called" ); array = array.dup; } int get(size_t offset) { return array[offset]; } void set(size_t offset, int value) { array[offset] = value; } auto getArray() { return array; } } void main() { auto w1 = Widget(10); auto w2 = Widget(10); w1.set(5, 100); w2.set(5, 42); assert(w1.get(5) == 100); auto wd1 = Widget(3); Widget wd2 = Widget(3); wd1.set(0, 1); wd1.set(1, 2); wd1.set(2, 3); assert( wd1.getArray == [1,2,3] ); assert( wd2.getArray == [0,0,0] ); void update(string message) { writeln(message, " wd1 = ", wd1, ", wd2 = ", wd2); } void delegate(string) print = &update; print("before"); wd2 = wd1; print("after"); assert( wd2.getArray == [1,2,3] ); }
Jan 21 2017
Simplified: import std.stdio; struct Widget { private int[] array; this(uint length) { array = new int[length]; writefln("ctor called : %s", array.ptr); } this(this) { writef( "this(this) called: %s", array.ptr ); array = array.dup; writefln(" -> %s", array.ptr); } } void main() { auto w1 = Widget(10); writeln(w1); } It's due to the copies that are made when calling writeln and other functions that it calls. Ali
Jan 21 2017
On 01/21/2017 03:19 PM, Ali Çehreli wrote:this(this) {TIL! Change the signature and it works without copies: this(const(this)) { // ... } How did I miss this for so long? Ali
Jan 21 2017
On 01/21/2017 03:36 PM, Ali Çehreli wrote:Change the signature and it works without copies: this(const(this)) { // ... }Ugh... :( It's not a post-blit. Then what is it? Ali
Jan 21 2017
On Sunday, 22 January 2017 at 00:31:38 UTC, Ali Çehreli wrote:On 01/21/2017 03:36 PM, Ali Çehreli wrote:This is a __ctor that takes another instance as param. The param name is not specified, i.e struct Foo { this(const(this)){} } is the same as struct Foo { this(const(this) param){} } since __PRETTY__FUNCTION__ in the function returns "Foo Foo.this(const(Foo) _param_0) ref" That's strange. Isn't it mentioned somewhere in the specification that within a function declaration, "this" resolves to the type of the current aggregate ?Change the signature and it works without copies: this(const(this)) { // ... }Ugh... :( It's not a post-blit. Then what is it? Ali
Jan 21 2017
On 01/21/2017 07:22 PM, Basile B. wrote:On Sunday, 22 January 2017 at 00:31:38 UTC, Ali Çehreli wrote:Wow! Thanks. I know about 'alias this' but this (pun!) is new to me. TIL indeed and WAT!!!! (four exclamation marks is right in this case. :o) ) import std.stdio; struct S { void foo(this a = () { static assert(is(this)); // 'this' is a type return S(); }()) { static assert(is(typeof(this))); // 'this' is an expression writeln("foo called for ", this); } // WAT!!!! } void main() { auto s = S(); s.foo(s); } AliOn 01/21/2017 03:36 PM, Ali Çehreli wrote:This is a __ctor that takes another instance as param. The param name is not specified, i.e struct Foo { this(const(this)){} } is the same as struct Foo { this(const(this) param){} } since __PRETTY__FUNCTION__ in the function returns "Foo Foo.this(const(Foo) _param_0) ref" That's strange. Isn't it mentioned somewhere in the specification that within a function declaration, "this" resolves to the type of the current aggregate ?Change the signature and it works without copies: this(const(this)) { // ... }Ugh... :( It's not a post-blit. Then what is it? Ali
Jan 21 2017
On Sunday, 22 January 2017 at 03:42:21 UTC, Ali Çehreli wrote:On 01/21/2017 07:22 PM, Basile B. wrote:Same mood. I'm quite astonished to see that something like that still can be discovered, years after the language creation.[...]Wow! Thanks. I know about 'alias this' but this (pun!) is new to me. TIL indeed and WAT!!!! (four exclamation marks is right in this case. :o) ) import std.stdio; struct S { void foo(this a = () { static assert(is(this)); // 'this' is a type return S(); }()) { static assert(is(typeof(this))); // 'this' is an expression writeln("foo called for ", this); } // WAT!!!! } void main() { auto s = S(); s.foo(s); } Ali
Jan 21 2017