Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
July 24, 2015 How do you make a copy TO and object when you're INSIDE of it? | ||||
---|---|---|---|---|
| ||||
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? |
July 24, 2015 Re: How do you make a copy TO and object when you're INSIDE of it? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Enjoys Math | 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
|
July 24, 2015 Re: How do you make a copy TO and object when you're INSIDE of it? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Enjoys Math | 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
|
July 24, 2015 Re: How do you make a copy TO and object when you're INSIDE of it? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Friday, 24 July 2015 at 03:12:43 UTC, Steven Schveighoffer wrote:
> On 7/23/15 9:30 PM, Enjoys Math wrote:
>>[...]
>
> 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
Thanks. That sounds like a good approach
|
July 24, 2015 Re: How do you make a copy TO and object when you're INSIDE of it? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Enjoys Math | 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; |
July 24, 2015 Re: How do you make a copy TO and object when you're INSIDE of it? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marc Schütz | 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
|
July 24, 2015 Re: How do you make a copy TO and object when you're INSIDE of it? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | 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:
>> 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.
Right, this is dangerous for classes.
|
Copyright © 1999-2021 by the D Language Foundation