Thread overview | ||||||
---|---|---|---|---|---|---|
|
January 22, 2010 op= | ||||
---|---|---|---|---|
| ||||
according to the spec, a op= b; is semantically equivalent to a = a op b; but this doesn't seem to be strictly true. for example: char c = 'a'; real r = 3.14; c = c + r; // error c += r; // accepted; seems to be doing c += floor(r); is this behavior intentional? |
January 22, 2010 Re: op= | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ellery Newcomer | Ellery Newcomer wrote: > according to the spec, > > a op= b; > > is semantically equivalent to > > a = a op b; > > but this doesn't seem to be strictly true. It's untrue in several ways. For example: SomeReferenceType a, b; a = a + b; // Creates new object. b += c; // Modifies an object in place. and of course: a = a = c; // Dual assignment. a == b; // Equality comparison. -- Rainer Deyke - rainerd@eldwood.com |
January 22, 2010 Re: op= | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ellery Newcomer | On Thu, 21 Jan 2010 22:44:24 -0500, Ellery Newcomer <ellery-newcomer@utulsa.edu> wrote:
> according to the spec,
>
> a op= b;
>
> is semantically equivalent to
>
> a = a op b;
>
> but this doesn't seem to be strictly true. for example:
>
> char c = 'a';
> real r = 3.14;
>
> c = c + r; // error
>
> c += r; // accepted; seems to be doing c += floor(r);
>
> is this behavior intentional?
There are two issues, I think recently discussed, a op= b is actually equivalent to:
a = a op cast(typeof(a))b;
when dealing with primitives. I'm not sure if this happens with user defined types.
And the second issue (if you want to get technical) is that a op= b is considered a different operation than a = a op b because one can manually optimize a op= b more than a = a op b. So technically the seemingly analogous calls can do something different. A good example is array appending, a ~= b is not equivalent to a = a ~ b, because the former may allocate in place and the latter always reallocates.
-Steve
|
January 23, 2010 Re: op= | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On 01/22/2010 12:23 PM, Steven Schveighoffer wrote: > On Thu, 21 Jan 2010 22:44:24 -0500, Ellery Newcomer > <ellery-newcomer@utulsa.edu> wrote: > >> according to the spec, >> >> a op= b; >> >> is semantically equivalent to >> >> a = a op b; >> >> but this doesn't seem to be strictly true. for example: >> >> char c = 'a'; >> real r = 3.14; >> >> c = c + r; // error >> >> c += r; // accepted; seems to be doing c += floor(r); >> >> is this behavior intentional? > > There are two issues, I think recently discussed, a op= b is actually > equivalent to: > > a = a op cast(typeof(a))b; > > when dealing with primitives. I'm not sure if this happens with user > defined types. > > And the second issue (if you want to get technical) is that a op= b is > considered a different operation than a = a op b because one can > manually optimize a op= b more than a = a op b. So technically the > seemingly analogous calls can do something different. A good example is > array appending, a ~= b is not equivalent to a = a ~ b, because the > former may allocate in place and the latter always reallocates. > > -Steve Alright, that makes sense enough, thanks. Spec needs to be changed, though http://d.puremagic.com/issues/show_bug.cgi?id=3735 |
Copyright © 1999-2021 by the D Language Foundation