digitalmars.D.learn - How do you make a copy TO and object when you're INSIDE of it?
- Enjoys Math (23/23) Jul 23 2015 Here's my code:
- Jonathan M Davis via Digitalmars-d-learn (3/26) Jul 23 2015 Assign to the members individually rather than the whole object at once.
- Steven Schveighoffer (16/38) Jul 23 2015 You're approaching this wrong. Do the lookup before deciding whether to
- Enjoys Math (3/20) Jul 23 2015 Thanks. That sounds like a good approach
- "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> (7/7) Jul 24 2015 Apart from what others have said, for a class `this` is the
- Steven Schveighoffer (5/11) Jul 24 2015 I'm quite certain this wouldn't copy the derived data. So be careful
- "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> (3/19) Jul 24 2015 Right, this is dangerous for classes.
Here's my code: module grammar; class Grammar(T : ulong) { this(const T[] str) { auto grammar = str in grammarCache; if (grammar) { this = grammar.dup; } else { this = approximateSmallestGrammar(str); grammarCache[str] = this.dup; } } static Grammar approximateSmallestGrammar(const T[] str) { return new Grammar(); } property Grammar dup() { } private: this() {} static Grammar[T[]] grammarCache; }; Compiler says 'this' is not an lvalue. How would I accomplish what I want?
Jul 23 2015
On Friday, July 24, 2015 01:30:55 Enjoys Math via Digitalmars-d-learn wrote:Here's my code: module grammar; class Grammar(T : ulong) { this(const T[] str) { auto grammar = str in grammarCache; if (grammar) { this = grammar.dup; } else { this = approximateSmallestGrammar(str); grammarCache[str] = this.dup; } } static Grammar approximateSmallestGrammar(const T[] str) { return new Grammar(); } property Grammar dup() { } private: this() {} static Grammar[T[]] grammarCache; }; Compiler says 'this' is not an lvalue. How would I accomplish what I want?Assign to the members individually rather than the whole object at once. - Jonathan M Davis
Jul 23 2015
On 7/23/15 9:30 PM, Enjoys Math wrote:Here's my code: module grammar; class Grammar(T : ulong) { this(const T[] str) { auto grammar = str in grammarCache; if (grammar) { this = grammar.dup; } else { this = approximateSmallestGrammar(str); grammarCache[str] = this.dup; } } static Grammar approximateSmallestGrammar(const T[] str) { return new Grammar(); } property Grammar dup() { } private: this() {} static Grammar[T[]] grammarCache; }; Compiler says 'this' is not an lvalue. How would I accomplish what I want?You're approaching this wrong. Do the lookup before deciding whether to instantiate a new object: static Grammar getGrammar(const T[] str) { if(auto x = str in grammarCache) return *x; else { auto g = new Grammar; grammarCache[str] = g; return g; } } If you always want to dup, then do it outside the lookup. Don't do it in the constructor, you already have an object by then. -Steve
Jul 23 2015
On Friday, 24 July 2015 at 03:12:43 UTC, Steven Schveighoffer wrote:On 7/23/15 9:30 PM, Enjoys Math wrote:Thanks. That sounds like a good approach[...]You're approaching this wrong. Do the lookup before deciding whether to instantiate a new object: static Grammar getGrammar(const T[] str) { if(auto x = str in grammarCache) return *x; else { auto g = new Grammar; grammarCache[str] = g; return g; } } If you always want to dup, then do it outside the lookup. Don't do it in the constructor, you already have an object by then. -Steve
Jul 23 2015
Apart from what others have said, for a class `this` is the _reference_ to the current object, i.e. a "pointer". If the compiler allowed assigning to it, it would not modify the contents of your object. If you want to assign all of the elements at once, you can use `tupleof` (untested): this.tupleof = other.tupleof;
Jul 24 2015
On 7/24/15 4:47 AM, "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net>" wrote:Apart from what others have said, for a class `this` is the _reference_ to the current object, i.e. a "pointer". If the compiler allowed assigning to it, it would not modify the contents of your object. If you want to assign all of the elements at once, you can use `tupleof` (untested): this.tupleof = other.tupleof;I'm quite certain this wouldn't copy the derived data. So be careful when doing this, you can only do this on final classes. -Steve
Jul 24 2015
On Friday, 24 July 2015 at 14:12:54 UTC, Steven Schveighoffer wrote:On 7/24/15 4:47 AM, "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net>" wrote:Right, this is dangerous for classes.Apart from what others have said, for a class `this` is the _reference_ to the current object, i.e. a "pointer". If the compiler allowed assigning to it, it would not modify the contents of your object. If you want to assign all of the elements at once, you can use `tupleof` (untested): this.tupleof = other.tupleof;I'm quite certain this wouldn't copy the derived data. So be careful when doing this, you can only do this on final classes.
Jul 24 2015