www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 2006] New: Appending empty array using ~= doesn't work

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2006

           Summary: Appending empty array using ~= doesn't work
           Product: D
           Version: 2.012
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: bartosz relisoft.com


I have an empty array of arrays of string aas. I append to it an empty array of
string. The result should be an array aas of length one. 

string [][] aas = [];
aas ~= cast (string []) [];
writefln ("%d", aas.length);
aas = aas ~ cast (string []) [];
writefln ("%d", aas.length);

It works correctly when using assignment and concatenation, but id doesn't when
using the combined operator ~=. In the latter case, the length of array remains
zero.


-- 
Apr 18 2008
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2006





------- Comment #1 from wbaxter gmail.com  2008-04-18 14:46 -------
That's funny I was just about to file this one myself a couple of days ago, BUT
then I decided that it probably isn't really a bug.

[] could be an empty array of any type.  So the compiler is interpreting it as
an empty string[][], not an empty string[].  You have a T[], you concat an
empty T[],  your original array isn't supposed to change.

I will agree it's not the most useful of behaviors, and the doc could certainly
be clearer about what will happen in such a case, but I don't think its
actually a bug.  It's just the compiler interpreting [] in a not-so-useful
manner.  If you type a literal "~= []" in the code then clearly you want
something to happen.

The workaround is to use "~= [[]]" instead.


-- 
Apr 18 2008
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2006


smjg iname.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |smjg iname.com
           Keywords|                            |wrong-code




------- Comment #2 from smjg iname.com  2008-11-21 15:08 -------
(In reply to comment #1)
 [] could be an empty array of any type.  So the compiler is interpreting it as
 an empty string[][], not an empty string[].  You have a T[], you concat an
 empty T[],  your original array isn't supposed to change.

But it has been explicitly cast to a string[]. How can a CastExpression be of any type other than that to which it is cast? --
Nov 21 2008
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2006





------- Comment #3 from wbaxter gmail.com  2008-12-22 19:34 -------
I just got bitten by this again.

float[][] arr;
arr ~= [1.0]; // ok, adds a new element (an array of length 1).
arr ~= []; // not ok, does nothing. :-(

The last line there does nothing, apparently because the compiler interprets it
to be an array of array that's empty, which is the least useful interpretation.
 So I find it unexpected that the compiler interprets it this way.  Once
again... even though I already ran into it once.  I just forgot because it
seems so silly for the compiler to choose the interpretation that it does.

At the very least I'd like the compiler to generate an error saying it doesn't
know how to interpret 'arr ~= []'.


-- 
Dec 22 2008
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2006


clugdbug yahoo.com.au changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Version|2.012                       |1.028




------- Comment #4 from clugdbug yahoo.com.au  2009-02-19 07:06 -------
Applies equally to D1.


-- 
Feb 19 2009
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2006


Denis Derman <denis.spir gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |denis.spir gmail.com


--- Comment #5 from Denis Derman <denis.spir gmail.com> 2011-01-28 06:39:21 PST
---
(In reply to comment #3)
 I just got bitten by this again.
 
 float[][] arr;
 arr ~= [1.0]; // ok, adds a new element (an array of length 1).
 arr ~= []; // not ok, does nothing. :-(
 
 The last line there does nothing, apparently because the compiler interprets it
 to be an array of array that's empty, which is the least useful interpretation.
  So I find it unexpected that the compiler interprets it this way.  Once
 again... even though I already ran into it once.  I just forgot because it
 seems so silly for the compiler to choose the interpretation that it does.
 
 At the very least I'd like the compiler to generate an error saying it doesn't
 know how to interpret 'arr ~= []'.

Yes, ambiguity that is bug-prone, because both interpretations can run, must yield compiler error. Denis -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jan 28 2011