www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Operator inconsistencies

reply Benjamin Herr <ben 0x539.de> writes:
Hello, D!

I was taught that overloaded operators should emulate their function on 
primitive data types.
With ints (and probably structs), a += b and a = a + b is essentially 
the same.
With objects, a += b is a.opAddAssign(b) or the reverse and a = a + b is 
a = a.opAdd(b), which is clearly different as the former cannot emulate 
the latter's reference assignment.
This leads me to the assumption that we lack a operator for valueish 
assignment that is different from the reference assignment one.
What am I missing?

On a completely unrelated note, how can I implement arraylike a[] = 
b[foo ... bar]?

   -ben (the newbie one)
Oct 23 2004
next sibling parent reply Tyro <ridimz_at yahoo.dot.com> writes:
Benjamin Herr wrote:
 Hello, D!
 
 I was taught that overloaded operators should emulate their function on 
 primitive data types.
 With ints (and probably structs), a += b and a = a + b is essentially 
 the same.
 With objects, a += b is a.opAddAssign(b) or the reverse and a = a + b is 
 a = a.opAdd(b), which is clearly different as the former cannot emulate 
 the latter's reference assignment.
 This leads me to the assumption that we lack a operator for valueish 
 assignment that is different from the reference assignment one.
 What am I missing?
 
 On a completely unrelated note, how can I implement arraylike a[] = 
 b[foo ... bar]?

a = b[foo .. bar]; Unless I completely misunderstood your question!
   -ben (the newbie one)

Oct 23 2004
parent Benjamin Herr <ben 0x539.de> writes:
Tyro wrote:
 Benjamin Herr wrote:
 how can I implement arraylike a[] = b[foo ... bar]?

a = b[foo .. bar]; Unless I completely misunderstood your question!

I think there is the subtle difference that a[] = b[..] copies while a = b makes a refer the same data as b. int main() { char[] a = "Hello, World".dup; char[] b, c; b = a[0 .. length]; c.length = a.length; c[] = a[0 .. length]; a[7 .. 12] = "Tyro!"; printf("b is: %.*s\n", b); printf("c is: %.*s\n", c); return 0; }
Oct 23 2004
prev sibling parent "Walter" <newshound digitalmars.com> writes:
"Benjamin Herr" <ben 0x539.de> wrote in message
news:clelpa$oae$1 digitaldaemon.com...
 Hello, D!

 I was taught that overloaded operators should emulate their function on
 primitive data types.
 With ints (and probably structs), a += b and a = a + b is essentially
 the same.
 With objects, a += b is a.opAddAssign(b) or the reverse and a = a + b is
 a = a.opAdd(b), which is clearly different as the former cannot emulate
 the latter's reference assignment.
 This leads me to the assumption that we lack a operator for valueish
 assignment that is different from the reference assignment one.
 What am I missing?

 On a completely unrelated note, how can I implement arraylike a[] =
 b[foo ... bar]?

Just overload the .. operator with opSlice and have it return an array.
Oct 30 2004