www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How do you make a copy TO and object when you're INSIDE of it?

reply "Enjoys Math" <enjoysmath gmail.com> writes:
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
next sibling parent Jonathan M Davis via Digitalmars-d-learn writes:
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
prev sibling next sibling parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
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
parent "Enjoys Math" <enjoysmath gmail.com> writes:
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
Jul 23 2015
prev sibling parent reply "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
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
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
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
parent "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
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.
Jul 24 2015