www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - What's wrong with this pucture [DMD v0.120] [bug]

reply "TechnoZeus" <TechnoZeus PeoplePC.com> writes:
I understand why this example won't compile...

//_______________________________//
char[] source = "This is just for testing purposes.";
char[4] dest;
void main()
{
 dest = source[8..12];
 assert(dest=="just");
}
//_______________________________//

...since the compiler would think I'm trying to change the identity of dest to
match source,
but why doesn't this one work?...
//_______________________________//
char[] source = "This is just for testing purposes.";
char[4] dest;
void main()
{
 dest = source[8..12].dup;
 assert(dest=="just");
}
//_______________________________//
...when this is obviously a value assignment of four array elements to a four
element array?
And why does this one compile?...
//_______________________________//
char[] dest = "This is just for testing purposes.";
char[4] source;
void main()
{
 dest = source[8..12].dup;
 assert(dest=="just");
}
//_______________________________//
...when it should be obvious to the compiler
that I can't assign the 8th through 11th array elements
of an array of 4 characters to anything?

Looks like a bug to me... or possibly two bugs.
I "think" what's happening here is that the compiler is not taking treating the
.dup as a value assignment at all,
but rather as the assignment of a reference to the duplicate array.
Accptable behavior, perhaps,
but terribly inconvenient since it gets in the way of doing something that
should be possible.

Better behavior in my opinion would be for the compiler to do a value
assignment in this case
which is obviously what was intended since a reference assignment is
impossible....
...but even better yet would be to allow me to simply use...

//_______________________________//
char[] source = "This is just for testing purposes.";
char[4] dest;
void main()
{
 dest := source[8..12];
 assert(dest=="just");
}
//_______________________________//

...with the := value assignment operator so there's no ambiguity.

As for the second possible bug,
perhaps it's not a bug at all since...
source[8..12]
...could simply return an array of zero elements when "source" is a static
array of 4 characters...
but I would have expected at least a warning, since the 4, 8, and 12 are all
constant litterals.

TZ
Apr 21 2005
next sibling parent =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
TechnoZeus wrote:

 but why doesn't this one work?...
 //_______________________________//
 char[] source = "This is just for testing purposes.";
 char[4] dest;
 void main()
 {
  dest = source[8..12].dup;
  assert(dest=="just");
 }
 //_______________________________//
 ...when this is obviously a value assignment of four array elements to a four
element array?
No, you are trying to reassign the reference to the static array. Try: char[] source = "This is just for testing purposes."; char[4] dest; void main() { dest[] = source[8..12]; assert(dest=="just"); }
 And why does this one compile?...
 //_______________________________//
 char[] dest = "This is just for testing purposes.";
 char[4] source;
 void main()
 {
  dest = source[8..12].dup;
  assert(dest=="just");
 }
 //_______________________________//
 ...when it should be obvious to the compiler
 that I can't assign the 8th through 11th array elements
 of an array of 4 characters to anything?
 
 Looks like a bug to me... or possibly two bugs.
"source[8]" should trigger a compiler error, just doesn't (yet) But the spec just says: (http://www.digitalmars.com/d/arrays.html) "Compilers should attempt to detect array bounds errors at compile time" Currently these are being caught at runtime: ArrayBoundsError c.d(5) Neither of these are bugs. --anders
Apr 22 2005
prev sibling parent reply Derek Parnell <derek psych.ward> writes:
On Fri, 22 Apr 2005 01:52:58 -0500, TechnoZeus wrote:

 I understand why this example won't compile...
 
 //_______________________________//
 char[] source = "This is just for testing purposes.";
 char[4] dest;
 void main()
 {
  dest = source[8..12];
  assert(dest=="just");
 }
 //_______________________________//
 
 ...since the compiler would think I'm trying to change the identity of dest to
match source,
Well sort of... I see it differently. 'dest' is a static array, and as such it's structure is different from the dynamic array 'source'. 'dest' occupies 4 bytes of RAM because of the '[4]'. By slicing 'source', you are just creating another dynamic array, and in general you cannot assign a dynamic array to a static array. If you want to copy the values of a dynamic array to a static array, you need this syntax ... dest[] = source[8..12];
 but why doesn't this one work?...
 //_______________________________//
 char[] source = "This is just for testing purposes.";
 char[4] dest;
 void main()
 {
  dest = source[8..12].dup;
  assert(dest=="just");
 }
 //_______________________________//
 ...when this is obviously a value assignment of four array elements to a four
element array?
Because it is identical in concept to the first example. The .dup just creates a duplicate of the slice (dynamic array) so you are still trying to assign a dynamic array to a static one.
 And why does this one compile?...
 //_______________________________//
 char[] dest = "This is just for testing purposes.";
 char[4] source;
 void main()
 {
  dest = source[8..12].dup;
  assert(dest=="just");
 }
 //_______________________________//

 ...when it should be obvious to the compiler
 that I can't assign the 8th through 11th array elements
 of an array of 4 characters to anything?
It compiles because array bounds checking is done at run time, not compile time. It doesn't run though.
 Looks like a bug to me... or possibly two bugs.
No, I don't think that there are any bugs here.
 I "think" what's happening here is that the compiler is not taking treating
the .dup as a value assignment at all,
 but rather as the assignment of a reference to the duplicate array.
 Accptable behavior, perhaps,
 but terribly inconvenient since it gets in the way of doing something that
should be possible.
 
 Better behavior in my opinion would be for the compiler to do a value
assignment in this case
 which is obviously what was intended since a reference assignment is
impossible....
 ...but even better yet would be to allow me to simply use...
But we already have syntax for copying the value of the source array... //_______________________________// char[] source = "This is just for testing purposes."; char[4] dest; void main() { dest[] = source[8..12]; assert(dest=="just"); } //_______________________________// The syntax 'dest[]' is shorthand for 'dest[0..$]'. -- Derek Parnell Melbourne, Australia http://www.dsource.org/projects/build/ v2.03 released 20/Apr/2005 http://www.prowiki.org/wiki4d/wiki.cgi?FrontPage 22/04/2005 5:22:22 PM
Apr 22 2005
parent "TechnoZeus" <TechnoZeus PeoplePC.com> writes:
"Derek Parnell" <derek psych.ward> wrote in message
news:1jn47ouwk2iii.1hy8ge92hie4j$.dlg 40tude.net...
 On Fri, 22 Apr 2005 01:52:58 -0500, TechnoZeus wrote:

 I understand why this example won't compile...

 //_______________________________//
 char[] source = "This is just for testing purposes.";
 char[4] dest;
 void main()
 {
  dest = source[8..12];
  assert(dest=="just");
 }
 //_______________________________//

 ...since the compiler would think I'm trying to change the identity of dest to
match source,
Well sort of... I see it differently. 'dest' is a static array, and as such it's structure is different from the dynamic array 'source'. 'dest' occupies 4 bytes of RAM because of the '[4]'. By slicing 'source', you are just creating another dynamic array, and in general you cannot assign a dynamic array to a static array. If you want to copy the values of a dynamic array to a static array, you need this syntax ... dest[] = source[8..12];
 but why doesn't this one work?...
 //_______________________________//
 char[] source = "This is just for testing purposes.";
 char[4] dest;
 void main()
 {
  dest = source[8..12].dup;
  assert(dest=="just");
 }
 //_______________________________//
 ...when this is obviously a value assignment of four array elements to a four
element array?
Because it is identical in concept to the first example. The .dup just creates a duplicate of the slice (dynamic array) so you are still trying to assign a dynamic array to a static one.
 And why does this one compile?...
 //_______________________________//
 char[] dest = "This is just for testing purposes.";
 char[4] source;
 void main()
 {
  dest = source[8..12].dup;
  assert(dest=="just");
 }
 //_______________________________//

 ...when it should be obvious to the compiler
 that I can't assign the 8th through 11th array elements
 of an array of 4 characters to anything?
It compiles because array bounds checking is done at run time, not compile time. It doesn't run though.
 Looks like a bug to me... or possibly two bugs.
No, I don't think that there are any bugs here.
 I "think" what's happening here is that the compiler is not taking treating
the .dup as a value assignment at all,
 but rather as the assignment of a reference to the duplicate array.
 Accptable behavior, perhaps,
 but terribly inconvenient since it gets in the way of doing something that
should be possible.

 Better behavior in my opinion would be for the compiler to do a value
assignment in this case
 which is obviously what was intended since a reference assignment is
impossible....
 ...but even better yet would be to allow me to simply use...
But we already have syntax for copying the value of the source array... //_______________________________// char[] source = "This is just for testing purposes."; char[4] dest; void main() { dest[] = source[8..12]; assert(dest=="just"); } //_______________________________// The syntax 'dest[]' is shorthand for 'dest[0..$]'. -- Derek Parnell Melbourne, Australia http://www.dsource.org/projects/build/ v2.03 released 20/Apr/2005 http://www.prowiki.org/wiki4d/wiki.cgi?FrontPage 22/04/2005 5:22:22 PM
Again, I see this as an example where a value assignment operator could remove the ambiguity. In the discussion about whether or not := should be added as a value assignment operator, one of the arguments against it being "needed" was that using ".dup" did the same thing. Obviously, it does not. TZ
Apr 22 2005