digitalmars.D - Re: Array literals
- bearophile <bearophileHUGS lycos.com> Oct 17 2008
- "Nick Sabalausky" <a a.a> Oct 17 2008
- Michel Fortin <michel.fortin michelf.com> Oct 18 2008
- KennyTM~ <kennytm gmail.com> Oct 18 2008
- Michel Fortin <michel.fortin michelf.com> Oct 18 2008
- KennyTM~ <kennytm gmail.com> Oct 18 2008
- Sergey Gromov <snake.scaly gmail.com> Oct 18 2008
Nick Sabalausky:I like T[static] a lot. Static might be a highly overloaded word, but the idea of a "static array" is already one of the firmly established overloads. You want a static array? Say "static". Nice :)
+1. I think it's acceptable. So the code equivalent to the original one becomes (plus the immutable statement): import std.stdio: writefln; void main() { char[static][static] a = ["Hello", "what"]; writefln(a[2].length); // 5 } Most of the times you don't want that so you use: auto a = ["Hello", "what"]; Now we can wait for Walter to express his opinion on this improvement of the language. Bye, bearophile
Oct 17 2008
"bearophile" <bearophileHUGS lycos.com> wrote in message news:gd9vqi$oos$1 digitalmars.com...Nick Sabalausky:I like T[static] a lot. Static might be a highly overloaded word, but the idea of a "static array" is already one of the firmly established overloads. You want a static array? Say "static". Nice :)
+1. I think it's acceptable. So the code equivalent to the original one becomes (plus the immutable statement): import std.stdio: writefln; void main() { char[static][static] a = ["Hello", "what"]; writefln(a[2].length); // 5 } Most of the times you don't want that so you use: auto a = ["Hello", "what"]; Now we can wait for Walter to express his opinion on this improvement of the language. Bye, bearophile
As long as we're talking about initializing jagged static arrays, I'd also add one other slight change. The following should also compile (the current D1 equivilent code complains. Not sure about D2): // The only change from the code above is // swapping the order of "Hello" and "what" import std.stdio: writefln; void main() { char[static][static] a = ["what", "Hello"]; writefln(a[1].length); // 5 } Might just be a bug, but currently (assuming the T[static] got implemented), the above would fail to compile with an error complaining that it cannot convert "Hello" from type char[5u] to type chat[4u]. Apperently, "a[anything].length" is assumed to be "a[0].length" instead of "max(a[0].length, a[1].length,...a[$-1].length)". So, currently, a[0].length must be >= the longest of the rest of the strings. That's bitten me a few times already.
Oct 17 2008
On 2008-10-17 16:00:55 -0400, "Nick Sabalausky" <a a.a> said:As long as we're talking about initializing jagged static arrays, I'd also add one other slight change. The following should also compile (the current D1 equivilent code complains. Not sure about D2): // The only change from the code above is // swapping the order of "Hello" and "what" import std.stdio: writefln; void main() { char[static][static] a = ["what", "Hello"]; writefln(a[1].length); // 5 } Might just be a bug, but currently (assuming the T[static] got implemented), the above would fail to compile with an error complaining that it cannot convert "Hello" from type char[5u] to type chat[4u]. Apperently, "a[anything].length" is assumed to be "a[0].length" instead of "max(a[0].length, a[1].length,...a[$-1].length)". So, currently, a[0].length must be >= the longest of the rest of the strings. That's bitten me a few times already.
For that to work, you'd also need implcit conversion from T[4] to T[5]. How are your proposing to do that? -- Michel Fortin michel.fortin michelf.com http://michelf.com/
Oct 18 2008
Michel Fortin wrote:On 2008-10-17 16:00:55 -0400, "Nick Sabalausky" <a a.a> said:As long as we're talking about initializing jagged static arrays, I'd also add one other slight change. The following should also compile (the current D1 equivilent code complains. Not sure about D2): // The only change from the code above is // swapping the order of "Hello" and "what" import std.stdio: writefln; void main() { char[static][static] a = ["what", "Hello"]; writefln(a[1].length); // 5 } Might just be a bug, but currently (assuming the T[static] got implemented), the above would fail to compile with an error complaining that it cannot convert "Hello" from type char[5u] to type chat[4u]. Apperently, "a[anything].length" is assumed to be "a[0].length" instead of "max(a[0].length, a[1].length,...a[$-1].length)". So, currently, a[0].length must be >= the longest of the rest of the strings. That's bitten me a few times already.
For that to work, you'd also need implcit conversion from T[4] to T[5]. How are your proposing to do that?
Pad with T.init
Oct 18 2008
On 2008-10-18 11:31:09 -0400, KennyTM~ <kennytm gmail.com> said:// The only change from the code above is // swapping the order of "Hello" and "what" import std.stdio: writefln; void main() { char[static][static] a = ["what", "Hello"]; writefln(a[1].length); // 5 } [...]
How are your proposing to do that?
Pad with T.init
Should I take it then that the following assertions should hold true? char[static][static] a = ["what", "Hello"]; assert(a[0] == "what\0"); assert(a[0] != "what"); That seems completly backward. -- Michel Fortin michel.fortin michelf.com http://michelf.com/
Oct 18 2008
Michel Fortin wrote:On 2008-10-18 11:31:09 -0400, KennyTM~ <kennytm gmail.com> said:// The only change from the code above is // swapping the order of "Hello" and "what" import std.stdio: writefln; void main() { char[static][static] a = ["what", "Hello"]; writefln(a[1].length); // 5 } [...]
T[5]. How are your proposing to do that?
Pad with T.init
Should I take it then that the following assertions should hold true? char[static][static] a = ["what", "Hello"]; assert(a[0] == "what\0"); assert(a[0] != "what"); That seems completly backward.
Isn't it the current behavior? auto x = ["World", "x"]; writeln(typeof(x[0]).stringof); writeln(typeof(x[1]).stringof); writeln(typeof(x).stringof); writeln(x[1] == "x"); prints: invariant(char[5u]) invariant(char[5u]) invariant(char[5u])[2u] false So at least it is just "not fixing an old bug" (if this is), but not "completely backward".
Oct 18 2008
Fri, 17 Oct 2008 16:00:55 -0400, Nick Sabalausky wrote:"bearophile" <bearophileHUGS lycos.com> wrote in message news:gd9vqi$oos$1 digitalmars.com...Nick Sabalausky:I like T[static] a lot. Static might be a highly overloaded word, but the idea of a "static array" is already one of the firmly established overloads. You want a static array? Say "static". Nice :)
+1. I think it's acceptable. So the code equivalent to the original one becomes (plus the immutable statement): import std.stdio: writefln; void main() { char[static][static] a = ["Hello", "what"]; writefln(a[2].length); // 5 } Most of the times you don't want that so you use: auto a = ["Hello", "what"]; Now we can wait for Walter to express his opinion on this improvement of the language. Bye, bearophile
As long as we're talking about initializing jagged static arrays, I'd also add one other slight change. The following should also compile (the current D1 equivilent code complains. Not sure about D2): // The only change from the code above is // swapping the order of "Hello" and "what" import std.stdio: writefln; void main() { char[static][static] a = ["what", "Hello"]; writefln(a[1].length); // 5 }
Type of an array element is inferred from the first element of a literal, it's in the specs. Also, compiler probably fills the missing elements with default initializer so in fact you get char[static][static] a = ["what\0", "Hello"]; or, for the same result, char[5][static] a = ["what", "Hello"]; It'd probably be nice if, for arrays of static arrays, compiler automatically picked the longest inner array for the purposes of outer array's element type inferring.
Oct 18 2008