digitalmars.D - Array Initialization
- h3r3tic (9/9) Jul 26 2004 Can anyone tell me why it isnt allowed in D to say:
- Sha Chancellor (15/21) Jul 26 2004 Not sure why you can't initialize a dynamic array with a static array.
- h3r3tic (18/31) Jul 26 2004 that's funny... I can do:
- Derek Parnell (33/67) Jul 26 2004 This is because the 'f' is initialized by the compiler/linker when creat...
- h3r3tic (10/50) Jul 26 2004 I know that... :/ but yeah, the point is that the compiler should take c...
-
Stewart Gordon
(10/16)
Jul 27 2004
- Ilya Minkov (19/30) Jul 27 2004 A nasty bug, i'd say. But it's well known, it's probably just not a
- Juanjo =?ISO-8859-15?Q?=C1lvarez?= (6/12) Jul 28 2004 I'm also a pythonist but I don't like array coprehensions -- I think the...
- Andy Friesen (29/45) Jul 28 2004 It could be that I have an overly optimistic view of what an 'average
Can anyone tell me why it isnt allowed in D to say: /*static*/ int[] foo = [1, 2, 3]; without that damn static keyword ? End even forgiving 'static', why one can initialize arrays only with compile-time known values ? And, from the array zone: Is there any chance that array comprehensions will be added to D some day ? I just love those in Python e.g. /*Python code*/ foo = [1.0, 2.0, 3.0]
Jul 26 2004
In article <ce2s7e$sih$1 digitaldaemon.com>, h3r3tic <h3r3tic dev.null> wrote:Can anyone tell me why it isnt allowed in D to say: /*static*/ int[] foo = [1, 2, 3]; without that damn static keyword ? End even forgiving 'static', why one can initialize arrays only with compile-time known values ?Not sure why you can't initialize a dynamic array with a static array. Pretty silly. You should be able to at least do int[3] foo = [1,2,3]; I can't even do that in GDC. This looks like a compiler bug according to desired behavior listed in: http://www.digitalmars.com/d/arrays.html By any chance are you using GDC or DMD? I'm using gdc and will test this on DMD later. You should not need the static keyword. Static initializers are for static *arrays* IE, not dynamically resized. For some reason it works as static foo[3] = [1,2,3]; but if you leave the static out it won't let you use a static initializer.
Jul 26 2004
Not sure why you can't initialize a dynamic array with a static array. Pretty silly. You should be able to at least do int[3] foo = [1,2,3]; I can't even do that in GDC. This looks like a compiler bug according to desired behavior listed in: http://www.digitalmars.com/d/arrays.html By any chance are you using GDC or DMD? I'm using gdc and will test this on DMD later.that's funny... I can do: <code> float[] f = [3.0, 5.0, 2.0, 4.0]; void main () { } </code> but not <code> void main () { float[] f = [3.0, 5.0, 2.0, 4.0]; } </code> I'm using DMD 0.96 But that's not what pisses me most. Why is the initializer STATIC ? I'd be cool to initialize an array with data that you calculate at runtime. C and C++ let you do that. For some awkward reason it's impossible in D
Jul 26 2004
On Tue, 27 Jul 2004 02:28:13 +0200, h3r3tic wrote:This is because the 'f' is initialized by the compiler/linker when creating the .exe file. The RAM is pre-initialized.Not sure why you can't initialize a dynamic array with a static array. Pretty silly. You should be able to at least do int[3] foo = [1,2,3]; I can't even do that in GDC. This looks like a compiler bug according to desired behavior listed in: http://www.digitalmars.com/d/arrays.html By any chance are you using GDC or DMD? I'm using gdc and will test this on DMD later.that's funny... I can do: <code> float[] f = [3.0, 5.0, 2.0, 4.0]; void main () { } </code>but not <code> void main () { float[] f = [3.0, 5.0, 2.0, 4.0]; } </code>This is because the 'f' here resides on the stack, which is not a known memory location until run-time. The initialization is done at run-time. However the real point, as I see it, is apart from the technicality, why doesn't the compiler help the coder and just do it automatically anyway? My guess is that D will eventually let one do this, but its not in the priority task list as it can be achieved using other methods.I'm using DMD 0.96 But that's not what pisses me most. Why is the initializer STATIC ? I'd be cool to initialize an array with data that you calculate at runtime. C and C++ let you do that. For some awkward reason it's impossible in Dvoid main () { float[] f = [3.0, 5.0, 2.0, 4.0]; } is equivalent to void main () { float[] f; f.length = 4; f[0] = 3.0; f[1] = 5.0; f[2] = 2.0; f[3] = 4.0; } Another way is to do this ... static float[] f_init = [3.0, 5.0, 2.0, 4.0]; void main () { float[] f = f_init.dup; } -- Derek Melbourne, Australia 27/Jul/04 11:47:01 AM
Jul 26 2004
In article <ce4cmt$1lvl$1 digitaldaemon.com>, Derek Parnell says...On Tue, 27 Jul 2004 02:28:13 +0200, h3r3tic wrote:I know that... :/ but yeah, the point is that the compiler should take care of it. I think it's a necessary feature for 1.0 as it's very comfortable to initialize arrays dynamically. However there's no note about that possibility anywhere in the docs. Just like the issue was non-existent. Does it mean : TODO ?that's funny... I can do: <code> float[] f = [3.0, 5.0, 2.0, 4.0]; void main () { } </code>This is because the 'f' is initialized by the compiler/linker when creating the .exe file. The RAM is pre-initialized.but not <code> void main () { float[] f = [3.0, 5.0, 2.0, 4.0]; } </code>This is because the 'f' here resides on the stack, which is not a known memory location until run-time. The initialization is done at run-time.void main () { float[] f = [3.0, 5.0, 2.0, 4.0]; } is equivalent to void main () { float[] f; f.length = 4; f[0] = 3.0; f[1] = 5.0; f[2] = 2.0; f[3] = 4.0; } Another way is to do this ... static float[] f_init = [3.0, 5.0, 2.0, 4.0]; void main () { float[] f = f_init.dup; }yeah, and that's pretty ugly and takes considerably more time and effort to code, doesn't it :) Walter, I think I can forgive you that limitation at the moment, but lets have array comprehensions in 2.0 ;)
Jul 26 2004
h3r3tic wrote:Can anyone tell me why it isnt allowed in D to say: /*static*/ int[] foo = [1, 2, 3]; without that damn static keyword ? End even forgiving 'static', why one can initialize arrays only with compile-time known values ?<snip> It's just an arbitrary restriction AFAICS. Inconsistent considering that it will let you initialise non-static class members in this way. http://www.digitalmars.com/drn-bin/wwwnews?D/26695 Stewart. -- My e-mail is valid but not my primary mailbox, aside from its being the unfortunate victim of intensive mail-bombing at the moment. Please keep replies on the 'group where everyone may benefit.
Jul 27 2004
h3r3tic schrieb:Can anyone tell me why it isnt allowed in D to say: /*static*/ int[] foo = [1, 2, 3];A nasty bug, i'd say. But it's well known, it's probably just not a priority. At the very least things like (in function body) int[] foo = [1, 2, 3].dup; should work, but they don't. What i do is declare a local const array, and dup it into a dynamic array.without that damn static keyword ? End even forgiving 'static', why one can initialize arrays only with compile-time known values ?That depends on whether you're talking of local variables or global ones. With local, it should just work - from another array, concatenations of arrays and values, or from a function output. With global, it's not that trivial, but i think D should create automatic mudule initializers to do the job. For example, on global level: RegExp reBlah = new RegExp("blah,""); doesn't work, but would be nice if it would. Even better, the same with type inferred: var reBlah = new RegExp("blah,"");And, from the array zone: Is there any chance that array comprehensions will be added to D some day ? I just love those in Python e.g. /*Python code*/ foo = [1.0, 2.0, 3.0]Hmmm... i think templates for such things would be appropriate. I don't see much chance for it in the language. But perhaps due to the extensions language planned in 2.0. -eye
Jul 27 2004
h3r3tic wrote:And, from the array zone: Is there any chance that array comprehensions will be added to D some day ? I just love those in Python e.g. /*Python code*/ foo = [1.0, 2.0, 3.0]I'm also a pythonist but I don't like array coprehensions -- I think they are a lot less readable for the average programmer than: foo = [1.2, 2.0, 3.0] for x in foo: bar.append( int(x) )
Jul 28 2004
Juanjo Álvarez wrote:h3r3tic wrote:It could be that I have an overly optimistic view of what an 'average programmer' can deduce from 'weird' code, but I find them greatly useful for expressing list transformations. For instance: reportCells = [] for row in all_rows: if row.date > a_date: outRow = [] for col in row: outRow.append(str(col)) reportCells.append(outRow) vs reportCells = [ [str(col) for col in row] for row in all_rows if row.date > a_date ] Both do the same thing, and neither is very complicated, but I think the second version does a better job of describing what the end result will look like. The first, in contrast, is stuck on the steps that must be taken to create it. Further, since the code is smaller from a visual standpoint, it doesn't attract as much attention from the reader. This is desirable because it helps make complicated things stand out. It also happens to map nicely to SQL. Back on topic, I doubt D will have list comprehensions of any sort for a long while yet. (cross your fingers for 3.0!) -- andyAnd, from the array zone: Is there any chance that array comprehensions will be added to D some day ? I just love those in Python e.g. /*Python code*/ foo = [1.0, 2.0, 3.0]I'm also a pythonist but I don't like array coprehensions -- I think they are a lot less readable for the average programmer than: foo = [1.2, 2.0, 3.0] for x in foo: bar.append( int(x) )
Jul 28 2004