digitalmars.D - Can I load an Dynamic Real Array on one line?
- David L. Davis <SpottedTiger yahoo.com> Jul 17 2004
- Andy Friesen <andy ikagames.com> Jul 17 2004
- "Matthew" <admin stlsoft.dot.dot.dot.dot.org> Jul 17 2004
- Regan Heath <regan netwin.co.nz> Jul 17 2004
- David L. Davis <SpottedTiger yahoo.com> Jul 17 2004
- Andy Friesen <andy ikagames.com> Jul 17 2004
- David L. Davis <SpottedTiger yahoo.com> Jul 19 2004
- Derek <derek psyc.ward> Jul 17 2004
- Ilya Minkov <minkov cs.tum.edu> Jul 22 2004
- Regan Heath <regan netwin.co.nz> Jul 22 2004
- David L. Davis <SpottedTiger yahoo.com> Jul 22 2004
- Ilya Minkov <minkov cs.tum.edu> Jul 23 2004
- Regan Heath <regan netwin.co.nz> Jul 23 2004
Could someone please point out what I'm doing incorrectly in trying to assign a
series of real values on one line into a dynamic real array?
# int main()
# {
# real rArr[];
#
# // This works fine
# rArr.length = 5;
# rArr[ 0 ] = -70_000;
# rArr[ 1 ] = 12_000;
# rArr[ 2 ] = 15_000;
# rArr[ 3 ] = 18_000;
# rArr[ 4 ] = 21_000;
#
# // In trying to do a shorthand method of loading the Dynamic array
# // with one line shown below. I'm getting a "expression expected,
# // not '['" and a "found ']' when expecting ';' following 'statement'"
# // set of errors.
# rArr.length = 4
# rArr[] = [ -200.0, 500.0, 324.0, -230.0 ];
#
# } // end int main()
Thxs for any help in advance! :)
-------------------------------------------------------------------
"Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"
Jul 17 2004
David L. Davis wrote:Could someone please point out what I'm doing incorrectly in trying to assign a series of real values on one line into a dynamic real array? # int main() # { # real rArr[]; # # // This works fine # rArr.length = 5; # rArr[ 0 ] = -70_000; # rArr[ 1 ] = 12_000; # rArr[ 2 ] = 15_000; # rArr[ 3 ] = 18_000; # rArr[ 4 ] = 21_000; # # // In trying to do a shorthand method of loading the Dynamic array # // with one line shown below. I'm getting a "expression expected, # // not '['" and a "found ']' when expecting ';' following 'statement'" # // set of errors. # rArr.length = 4 # rArr[] = [ -200.0, 500.0, 324.0, -230.0 ]; # # } // end int main() Thxs for any help in advance! :)
Maybe this needs to be added to Phobos. ;) import std.stdarg; template makeArray(T) { T[] makeArray(...) { T[] result; result.length = _arguments.length; for (int index, TypeInfo ti; _arguments) { // FIXME: handle derived class types and interfaces correctly. assert(ti === typeid(T)); result[index] = va_arg!(T)(_argptr); } } } Then you can just do: real[] realArray = makeArray!(real)(-70_000, 12_000, 15_000, 18_000, 21_000); -- andy
Jul 17 2004
"Andy Friesen" <andy ikagames.com> wrote in message news:cdbvuh$1im0$1 digitaldaemon.com...David L. Davis wrote:Could someone please point out what I'm doing incorrectly in trying to assign
series of real values on one line into a dynamic real array? # int main() # { # real rArr[]; # # // This works fine # rArr.length = 5; # rArr[ 0 ] = -70_000; # rArr[ 1 ] = 12_000; # rArr[ 2 ] = 15_000; # rArr[ 3 ] = 18_000; # rArr[ 4 ] = 21_000; # # // In trying to do a shorthand method of loading the Dynamic array # // with one line shown below. I'm getting a "expression expected, # // not '['" and a "found ']' when expecting ';' following 'statement'" # // set of errors. # rArr.length = 4 # rArr[] = [ -200.0, 500.0, 324.0, -230.0 ]; # # } // end int main() Thxs for any help in advance! :)
Maybe this needs to be added to Phobos. ;) import std.stdarg; template makeArray(T) { T[] makeArray(...) { T[] result; result.length = _arguments.length; for (int index, TypeInfo ti; _arguments) { // FIXME: handle derived class types and interfaces correctly. assert(ti === typeid(T)); result[index] = va_arg!(T)(_argptr); } } } Then you can just do: real[] realArray = makeArray!(real)(-70_000, 12_000, 15_000, 18_000, 21_000);
If it's not added to Phobos, can we put this in dtl.utility.arrays; ? Please remind me of this once DTL 0.1 is out there.
Jul 17 2004
On Sun, 18 Jul 2004 06:40:07 +1000, Matthew <admin stlsoft.dot.dot.dot.dot.org> wrote:"Andy Friesen" <andy ikagames.com> wrote in message news:cdbvuh$1im0$1 digitaldaemon.com...David L. Davis wrote:Could someone please point out what I'm doing incorrectly in trying
series of real values on one line into a dynamic real array? # int main() # { # real rArr[]; # # // This works fine # rArr.length = 5; # rArr[ 0 ] = -70_000; # rArr[ 1 ] = 12_000; # rArr[ 2 ] = 15_000; # rArr[ 3 ] = 18_000; # rArr[ 4 ] = 21_000; # # // In trying to do a shorthand method of loading the Dynamic
# // with one line shown below. I'm getting a "expression expected, # // not '['" and a "found ']' when expecting ';' following
# // set of errors. # rArr.length = 4 # rArr[] = [ -200.0, 500.0, 324.0, -230.0 ]; # # } // end int main() Thxs for any help in advance! :)
Maybe this needs to be added to Phobos. ;) import std.stdarg; template makeArray(T) { T[] makeArray(...) { T[] result; result.length = _arguments.length; for (int index, TypeInfo ti; _arguments) { // FIXME: handle derived class types and interfaces correctly. assert(ti === typeid(T)); result[index] = va_arg!(T)(_argptr); } } } Then you can just do: real[] realArray = makeArray!(real)(-70_000, 12_000, 15_000, 18_000, 21_000);
If it's not added to Phobos, can we put this in dtl.utility.arrays; ? Please remind me of this once DTL 0.1 is out there.
It should be mentioned that the original poster required an 'array literal' which Walter has hinted will be possible eventually. I believe the stumbling blocks include not being able to find a 'nice' syntax (which is also unambiguous). Regan -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jul 17 2004
Andy Friesen: Thanks for the reply and the template. Well, I hate to bug you,
but I can't seem to get it to work correctly, could please take another looks at
it?
First problem I ran into soon after I fixed the "for" to a "foreach", was that
it appears that writefln() and the template both define va_arg() differently, so
I get an error trying to use both...thus I moved to using printf() which
surprising works under std.stdio instead forcing std.c.stdio to be imported.
Secondly I had a problem with the assert( ti === typeid( T ) ) statement firing
off, meaning ti and typeid( T ) aren't equal to one another.
And then the Third problem poped up after I commented out the assert()
statement, in which of the four real numbers -200.0, 500.0, 324.0, -230.0 passed
into the template, only -200. and -230.0 showed up in the final array (as shown
below). Please note that -230.0 was suppose to be the last value in the array.
C:\dmd>dynarray
_arguments.length=4
typeid( real )=4257752, ti=4256136, result[ 0 ]= -200.0000
typeid( real )=4257752, ti=4256136, result[ 1 ]= 0.0000
typeid( real )=4257752, ti=4256136, result[ 2 ]= -230.0000
typeid( real )=4257752, ti=4256136, result[ 3 ]= 0.0000
I would really like to get this to work if all possible. Plus it just seems like
such a very "D" way of doing things "rArr[] = [ list of values, ... ];", but
that's me.
Thanks for your help.
# import std.stdarg;
# import std.stdio;
#
# template makeArray( T )
# {
# T[] makeArray(...)
# {
# T[] result;
#
# result.length = _arguments.length;
# printf( "_arguments.length=%d\n", _arguments.length );
#
# foreach ( int index, TypeInfo ti; _arguments )
# {
# // FIXME: handle derived class types and interfaces correctly.
# /+
# ' ti doesn't equal typeid( T )
# +/
# //assert( ti === typeid( T ) );
#
# result[ index ] = std.stdarg.va_arg!( T )( _argptr );
#
# printf( "typeid( real )=%ld, ti=%ld, result[ %d ]=%10.4f\n",
# typeid( real ), ti, index, result[ index ] );
# }
#
# return result[];
# }
#
# } // end template makeArray( T )
#
# int main()
# {
# real rArr[];
#
# // This works fine
# rArr.length = 5;
# rArr[ 0 ] = -70_000;
# rArr[ 1 ] = 12_000;
# rArr[ 2 ] = 15_000;
# rArr[ 3 ] = 18_000;
# rArr[ 4 ] = 21_000;
#
# rArr = makeArray!(real)( -200.0, 500.0, 324.0, -230.0 );
#
# /+
# ' When I try to use the writefln() function with std.stdarg I get the
# ' follwing error mesage:
# '
# ' C:\dmd\bin\..\lib\phobos.lib(format) Offset 1380CH Record Type 00C3
# ' Error 1: Previous Definition Different :
_D3std6stdarg8va_arg_e6va_argFKPvZe
# ' --- errorlevel 1
# +/
# //std.stdio.writefln( "rArr[].length=%d", rArr[].length );
#
# return 0;
#
# } // end int main()
-------------------------------------------------------------------
"Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"
Jul 17 2004
David L. Davis wrote:Andy Friesen: Thanks for the reply and the template. Well, I hate to bug you, but I can't seem to get it to work correctly, could please take another looks at it? Secondly I had a problem with the assert( ti === typeid( T ) ) statement firing off, meaning ti and typeid( T ) aren't equal to one another. And then the Third problem poped up after I commented out the assert() statement, in which of the four real numbers -200.0, 500.0, 324.0, -230.0 passed into the template, only -200. and -230.0 showed up in the final array (as shown below). Please note that -230.0 was suppose to be the last value in the array.
There's a few things going on here. The big one is that the assert is there for a reason. ;) If it trips, then the types don't match. Suffixing the arguments with an L makes them reals, which causes the assertion to pass, and the return value to look like it ought to. eg. rArr = makeArray!(real)(-200.0L, 500.0L, 324.0L, -230.0L); The link conflict bug is mystifying. I can only assume it's a bug in the compiler. -- andy
Jul 17 2004
Andy Friesen: I've only been able to get the template to work so far with only
char[]s as long as I add a NULL to the string, but even this is pretty useful!
(Thought I'd post what I found thus far.)
Thanks again for your replys.
---- Console Output -------------------
C:\dmd>dynarray
sArr[] = makeArray!(char[])( "-200.0\0", "1200.0\0", "25.56\0" );
makeArray.result.length=3
makeArray.result[ 0 ] = -200.0
makeArray.result[ 1 ] = 1200.0
makeArray.result[ 2 ] = 25.56
returning...
main.sArr.length=3
main.sArr[ 0 ] = -200.0
main.sArr[ 1 ] = 1200.0
main.sArr[ 2 ] = 25.56
C:\dmd>
----------------------------------------
# //-- DynArray.d --
# import std.stdarg;
# import std.stdio;
#
# template makeArray( T )
# {
#
# T[] makeArray( ... )
# {
# T[] result;
#
# result.length = _arguments.length;
# printf( "makeArray.result.length=%d\n", _arguments.length );
#
# foreach ( int index, TypeInfo ti; _arguments )
# {
# // FIXME: handle derived class types and interfaces correctly.
# assert( ti === typeid( T ) );
#
# // Had to add this break point to make it work.
# if ( index > result.length - 1 ) break;
#
# result[ index ] = va_arg!(T)( _argptr );
# printf( "makeArray.result[ %d ] = %.*s\n", index, result[ index ]
);
# }
#
# printf( " returning...\n" );
# return result[];
# }
# }
#
# int main()
# {
# char[] sArr[];
#
# printf( "sArr[] = makeArray!(char[])( \"-200.0\\0\", \"1200.0\\0\",
\"25.56\\0\" );\n\n" );
# sArr.length = 3;
# sArr[] = makeArray!(char[])( "-200.0\0", "1200.0\0", "25.56\0" );
#
# printf( "\n" );
# printf( "main.sArr.length=%d\n", sArr.length );
#
# for ( int ix = 0; ix < sArr.length; ix++ )
# {
# printf( "main.sArr[ %d ] = %.*s\n", ix, sArr[ ix ] );
# }
#
# return 0;
#}
-------------------------------------------------------------------
"Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"
Jul 19 2004
On Sat, 17 Jul 2004 19:33:05 +0000 (UTC), David L. Davis wrote:Could someone please point out what I'm doing incorrectly in trying to assign a series of real values on one line into a dynamic real array? # int main() # { # real rArr[]; # # // This works fine # rArr.length = 5; # rArr[ 0 ] = -70_000; # rArr[ 1 ] = 12_000; # rArr[ 2 ] = 15_000; # rArr[ 3 ] = 18_000; # rArr[ 4 ] = 21_000; # # // In trying to do a shorthand method of loading the Dynamic array # // with one line shown below. I'm getting a "expression expected, # // not '['" and a "found ']' when expecting ';' following 'statement'" # // set of errors. # rArr.length = 4 # rArr[] = [ -200.0, 500.0, 324.0, -230.0 ]; # # } // end int main() Thxs for any help in advance! :)
I think its because D has not (yet?) implemented array literals. Thus any array initializations like this example, need to be done at run time rather than compile time. -- Derek Melbourne, Australia
Jul 17 2004
This is a well known "feature" in the compiler, though i believe leaving
this undocumented suxx. I do this (although it's one line too many):
int main() {
// Lay out the data
const real [] arr_prototype = [ -200.0, 500.0, 324.0, -230.0 ];
// Duplicate into a dynamic array
real arr[] = arr_prototype.dup;
// Make sure that assignment has taken place, just for demostration
assert (arr.length==4);
return 0;
}
Naturally, you can leave out the .dup and do a simple assignment, but
then you may not write anything into arr because it would be aliased
with arr_prototype, which is stored in a constant area.
Another thing you could make differently is have arr_prototype be a
static instead of const, but then you could occasionaly modify a global
variable, and that may not be what you want.
-eye
David L. Davis schrieb:
Could someone please point out what I'm doing incorrectly in trying to assign a
series of real values on one line into a dynamic real array?
# int main()
# {
# real rArr[];
#
# // This works fine
# rArr.length = 5;
# rArr[ 0 ] = -70_000;
# rArr[ 1 ] = 12_000;
# rArr[ 2 ] = 15_000;
# rArr[ 3 ] = 18_000;
# rArr[ 4 ] = 21_000;
#
# // In trying to do a shorthand method of loading the Dynamic array
# // with one line shown below. I'm getting a "expression expected,
# // not '['" and a "found ']' when expecting ';' following 'statement'"
# // set of errors.
# rArr.length = 4
# rArr[] = [ -200.0, 500.0, 324.0, -230.0 ];
#
# } // end int main()
Thxs for any help in advance! :)
-------------------------------------------------------------------
"Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"
Jul 22 2004
On Thu, 22 Jul 2004 16:19:48 +0200, Ilya Minkov <minkov cs.tum.edu> wrote:This is a well known "feature" in the compiler, though i believe leaving this undocumented suxx. I do this (although it's one line too many): int main() { // Lay out the data const real [] arr_prototype = [ -200.0, 500.0, 324.0, -230.0 ]; // Duplicate into a dynamic array real arr[] = arr_prototype.dup; // Make sure that assignment has taken place, just for demostration assert (arr.length==4); return 0; } Naturally, you can leave out the .dup and do a simple assignment, but then you may not write anything into arr because it would be aliased with arr_prototype, which is stored in a constant area.
No it's not :) This is the problem I have with 'const' currently, in your example the array _reference_ arr_prototype is const, but the data it contains/references is not. example: int main() { // Lay out the data const real[] arr_prototype = [ -200.0, 500.0, 324.0, -230.0 ]; // Duplicate into a dynamic array //real arr[] = arr_prototype.dup; //you used C style declaration? real[] arr = arr_prototype; // Make sure that assignment has taken place, just for demostration assert (arr.length == 4); arr[0] = 12; assert (arr[0] == arr_prototype[0]); return 0; } ReganAnother thing you could make differently is have arr_prototype be a static instead of const, but then you could occasionaly modify a global variable, and that may not be what you want. -eye David L. Davis schrieb:Could someone please point out what I'm doing incorrectly in trying to assign a series of real values on one line into a dynamic real array? # int main() # { # real rArr[]; # # // This works fine # rArr.length = 5; # rArr[ 0 ] = -70_000; # rArr[ 1 ] = 12_000; # rArr[ 2 ] = 15_000; # rArr[ 3 ] = 18_000; # rArr[ 4 ] = 21_000; # # // In trying to do a shorthand method of loading the Dynamic array # // with one line shown below. I'm getting a "expression expected, # // not '['" and a "found ']' when expecting ';' following 'statement'" # // set of errors. # rArr.length = 4 # rArr[] = [ -200.0, 500.0, 324.0, -230.0 ]; # # } // end int main() Thxs for any help in advance! :) ------------------------------------------------------------------- "Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"
-- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jul 22 2004
Ilya Minkov and Regan Heath: Thxs for your replys on this subject! :) ------------------------------------------------------------------- "Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"
Jul 22 2004
Regan Heath schrieb:Naturally, you can leave out the .dup and do a simple assignment, but then you may not write anything into arr because it would be aliased with arr_prototype, which is stored in a constant area.
No it's not :)
OK, even if it's not it's illegal. it just doesn't always work as intended.This is the problem I have with 'const' currently, in your example the array _reference_ arr_prototype is const, but the data it contains/references is not.
//real arr[] = arr_prototype.dup; //you used C style declaration? real[] arr = arr_prototype;
whoops! i would never write that myself, it's here because i first copied David's code verbatim. :) -eye
Jul 23 2004
On Fri, 23 Jul 2004 14:10:53 +0200, Ilya Minkov <minkov cs.tum.edu> wrote:Regan Heath schrieb:Naturally, you can leave out the .dup and do a simple assignment, but then you may not write anything into arr because it would be aliased with arr_prototype, which is stored in a constant area.
No it's not :)
OK, even if it's not it's illegal. it just doesn't always work as intended.
It has _never_ 'worked' for me, as in, given me a compile time or runtime error saying I cannot modify that const data. I think it's been implemented as I desribed in that "const int[]" means a constant reference to an array of non constant data.This is the problem I have with 'const' currently, in your example the array _reference_ arr_prototype is const, but the data it contains/references is not.
//real arr[] = arr_prototype.dup; //you used C style declaration? real[] arr = arr_prototype;
whoops! i would never write that myself, it's here because i first copied David's code verbatim. :)
Just checking. :) Regan -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jul 23 2004









Regan Heath <regan netwin.co.nz> 