digitalmars.D.learn - D Cookbook range save question
- mark (7/7) Jan 30 2020 In the D Cookbook it has as part of the FibonacciRange example:
- mark (4/11) Jan 30 2020 Oh, I understand now...
- Adam D. Ruppe (11/13) Jan 30 2020 (I'll answer here anyway just in case someone lands here via a
In the D Cookbook it has as part of the FibonacciRange example: property FibonacciRange save() { return this; } And in the description it says: "...save, which returns a new range that is a copy of the current range and can be advanced independently..." Why is this a *copy*? (For a copy (in C++) I'd have expected return *this.)
Jan 30 2020
On Thursday, 30 January 2020 at 10:31:08 UTC, mark wrote:In the D Cookbook it has as part of the FibonacciRange example: property FibonacciRange save() { return this; } And in the description it says: "...save, which returns a new range that is a copy of the current range and can be advanced independently..." Why is this a *copy*? (For a copy (in C++) I'd have expected return *this.)Oh, I understand now... Sorry for the noise but I don't know how to delete a premature post!
Jan 30 2020
(I'll answer here anyway just in case someone lands here via a web search.) On Thursday, 30 January 2020 at 10:31:08 UTC, mark wrote:Why is this a *copy*? (For a copy (in C++) I'd have expected return *this.)In C++, `this` is a pointer, but in D, it is a reference. So assignment follows those semantics instead; `Foo a = this` does a copy assignment and if you want a pointer, you need to explicitly ask for one with the & operator: `Foo* a = &this;` fun fact, old versions of D, ~11ish years ago, actually worked the same way as C++. But the reference one was generally nicer and you can turn it back to the pointer as needed so it got changed.
Jan 30 2020