digitalmars.D - Array literals
- bearophile (14/14) Oct 15 2008 By default arrays/strings in D1 are static:
- bearophile (5/7) Oct 15 2008 That was of course:
- Steven Schveighoffer (6/17) Oct 15 2008 I think this can be solved even simpler. Make string literal type be
- bearophile (6/9) Oct 15 2008 With your idea this syntax:
- Steven Schveighoffer (6/15) Oct 15 2008 Sure, make the type dynamic for all array literals. My issue is with yo...
- Sergey Gromov (12/30) Oct 16 2008 I can see one need, matrix literals:
- Steven Schveighoffer (10/44) Oct 16 2008 Just don't use auto:
- Andrei Alexandrescu (5/52) Oct 16 2008 Walter wanted to do things that way. I wanted to implement things the
- Steven Schveighoffer (12/64) Oct 16 2008 Yes, good point.
- Andrei Alexandrescu (3/73) Oct 16 2008 T[$] was also suggested.
- Nick Sabalausky (11/85) Oct 17 2008 I like T[static] a lot. Static might be a highly overloaded word, but th...
- Michel Fortin (10/11) Oct 18 2008 That looks much better than T[auto]. Because auto usually replaces a
- bearophile (3/3) Oct 16 2008 Seeing how very few people care of this topic I presume I'm the only one...
- ore-sama (2/4) Oct 16 2008 http://d.puremagic.com/issues/show_bug.cgi?id=2367#c2
- bearophile (7/9) Oct 16 2008 Yes, sorry, I was a little nervous for an unrelated thing :-)
- bearophile (4/5) Oct 16 2008 I find it ironic that 100+ messages are exchanged on this NG about somet...
- Andrei Alexandrescu (7/18) Oct 16 2008 I hear you. For the languages I know that has always been the case: one
- bearophile (14/20) Oct 16 2008 - I have seen that to design a language you have to take hundreds or tho...
- Andrei Alexandrescu (7/25) Oct 16 2008 Yah Walter does plan to make "string literals" dynamically-sized by
- Denis Koroskin (3/28) Oct 16 2008 Good news!
- bearophile (4/9) Oct 16 2008 Good. But why not make all array literals dynamically-sized by default ,...
- Andrei Alexandrescu (4/15) Oct 16 2008 I agree. Walter didn't like that though, but I don't remember his
- Sean Kelly (3/6) Oct 16 2008 Huzzah!
- Chad J (3/5) Oct 16 2008 YES
- bearophile (13/16) Oct 17 2008 +1.
- Nick Sabalausky (19/38) Oct 17 2008 As long as we're talking about initializing jagged static arrays, I'd al...
- Michel Fortin (7/26) Oct 18 2008 For that to work, you'd also need implcit conversion from T[4] to T[5].
- KennyTM~ (2/31) Oct 18 2008 Pad with T.init
- Michel Fortin (10/23) Oct 18 2008 Should I take it then that the following assertions should hold true?
- KennyTM~ (14/38) Oct 18 2008 Isn't it the current behavior?
- Sergey Gromov (11/50) Oct 18 2008 Type of an array element is inferred from the first element of a literal...
By default arrays/strings in D1 are static: import std.stdio: writefln; void main() { auto a = ["Hello", "what"]; writefln(a[2].length); // 5 } To remove a significant amount of bugs from my code, like that one (the second string is a static array of length 5) I suggest to "invert" the meaning of array literals: by default they define dynamic arrays/strings allocated on the heap (immutable too, if necessary). So a different and explicit syntax can be used/invented to denote static arrays. So this literal defines a dynamic array of heap-allocated strings: auto a = ["Hello", "what"]; A possible syntax to allocate a fixed size array of (immutable) fixed size arrays of chars: string[auto][auto] a = ["Hello", "what"]; This goes well with the usual D philosophy of "safety". Bye, bearophile
Oct 15 2008
bearophile:A possible syntax to allocate a fixed size array of (immutable) fixed size arrays of chars: string[auto][auto] a = ["Hello", "what"];That was of course: char[auto][auto] a = ["Hello", "what"]; Bye, bearophile
Oct 15 2008
"bearophile" wroteBy default arrays/strings in D1 are static: import std.stdio: writefln; void main() { auto a = ["Hello", "what"]; writefln(a[2].length); // 5 } To remove a significant amount of bugs from my code, like that one (the second string is a static array of length 5) I suggest to "invert" the meaning of array literals: by default they define dynamic arrays/strings allocated on the heap (immutable too, if necessary). So a different and explicit syntax can be used/invented to denote static arrays.I think this can be solved even simpler. Make string literal type be invariant(char)[] instead of static array. It does not need to be on the heap. That would solve lots of IFTI problems too. And probably no way this gets into D1... -Steve
Oct 15 2008
Steven Schveighoffer:I think this can be solved even simpler. Make string literal type be invariant(char)[] instead of static array. It does not need to be on the heap. That would solve lots of IFTI problems too.With your idea this syntax: auto a = ["Hello", "what"]; Gives a fixed-sized array of invariant(char)[]. I think that's asymmetric. My suggestion works with all arrays, so the array 'a' too becomes/is dynamic, keeping the simmetry. Bye, bearophile
Oct 15 2008
"bearophile" <bearophileHUGS lycos.com> wrote in message news:gd63sv$2j0t$1 digitalmars.com...Steven Schveighoffer:Sure, make the type dynamic for all array literals. My issue is with your proposal to make the data allocated on the heap. There is no need for an array literal to be typed as a static array. Ever. -SteveI think this can be solved even simpler. Make string literal type be invariant(char)[] instead of static array. It does not need to be on the heap. That would solve lots of IFTI problems too.With your idea this syntax: auto a = ["Hello", "what"]; Gives a fixed-sized array of invariant(char)[]. I think that's asymmetric. My suggestion works with all arrays, so the array 'a' too becomes/is dynamic, keeping the simmetry.
Oct 15 2008
Wed, 15 Oct 2008 21:12:31 -0400, Steven Schveighoffer wrote:"bearophile" <bearophileHUGS lycos.com> wrote in message news:gd63sv$2j0t$1 digitalmars.com...I can see one need, matrix literals: auto blah = [ [ 1, 0, 0 ], [ 0, 0, -1 ], [ 0, -1, 0 ] ]; so that blah is a sequence of 9 numbers accessed accordingly, not 3 arrays of arbitrary length each. Well, it's probably a special case and deserves a special, safer syntax.Steven Schveighoffer:Sure, make the type dynamic for all array literals. My issue is with your proposal to make the data allocated on the heap. There is no need for an array literal to be typed as a static array. Ever.I think this can be solved even simpler. Make string literal type be invariant(char)[] instead of static array. It does not need to be on the heap. That would solve lots of IFTI problems too.With your idea this syntax: auto a = ["Hello", "what"]; Gives a fixed-sized array of invariant(char)[]. I think that's asymmetric. My suggestion works with all arrays, so the array 'a' too becomes/is dynamic, keeping the simmetry.
Oct 16 2008
"Sergey Gromov" <snake.scaly gmail.com> wrote in message news:MPG.23615f5e560d4d3f989763 news.digitalmars.com...Wed, 15 Oct 2008 21:12:31 -0400, Steven Schveighoffer wrote:Just don't use auto: int[3][3] blah = ... Make it clear your intention. A literal should follow the most common usage, and leave the corner cases to specific syntax. Most people don't use static arrays when working with literals. The most common usage of static arrays I've seen is to establish a buffer on the stack: byte[1000] buf = void; -Steve"bearophile" <bearophileHUGS lycos.com> wrote in message news:gd63sv$2j0t$1 digitalmars.com...I can see one need, matrix literals: auto blah = [ [ 1, 0, 0 ], [ 0, 0, -1 ], [ 0, -1, 0 ] ]; so that blah is a sequence of 9 numbers accessed accordingly, not 3 arrays of arbitrary length each. Well, it's probably a special case and deserves a special, safer syntax.Steven Schveighoffer:Sure, make the type dynamic for all array literals. My issue is with your proposal to make the data allocated on the heap. There is no need for an array literal to be typed as a static array. Ever.I think this can be solved even simpler. Make string literal type be invariant(char)[] instead of static array. It does not need to be on the heap. That would solve lots of IFTI problems too.With your idea this syntax: auto a = ["Hello", "what"]; Gives a fixed-sized array of invariant(char)[]. I think that's asymmetric. My suggestion works with all arrays, so the array 'a' too becomes/is dynamic, keeping the simmetry.
Oct 16 2008
Steven Schveighoffer wrote:"Sergey Gromov" <snake.scaly gmail.com> wrote in message news:MPG.23615f5e560d4d3f989763 news.digitalmars.com...Walter wanted to do things that way. I wanted to implement things the T[auto] way. The problem with specifying size twice is that there are two points of maintenance. If the language can help, why not? AndreiWed, 15 Oct 2008 21:12:31 -0400, Steven Schveighoffer wrote:Just don't use auto: int[3][3] blah = ... Make it clear your intention. A literal should follow the most common usage, and leave the corner cases to specific syntax. Most people don't use static arrays when working with literals. The most common usage of static arrays I've seen is to establish a buffer on the stack: byte[1000] buf = void;"bearophile" <bearophileHUGS lycos.com> wrote in message news:gd63sv$2j0t$1 digitalmars.com...I can see one need, matrix literals: auto blah = [ [ 1, 0, 0 ], [ 0, 0, -1 ], [ 0, -1, 0 ] ]; so that blah is a sequence of 9 numbers accessed accordingly, not 3 arrays of arbitrary length each. Well, it's probably a special case and deserves a special, safer syntax.Steven Schveighoffer:Sure, make the type dynamic for all array literals. My issue is with your proposal to make the data allocated on the heap. There is no need for an array literal to be typed as a static array. Ever.I think this can be solved even simpler. Make string literal type be invariant(char)[] instead of static array. It does not need to be on the heap. That would solve lots of IFTI problems too.With your idea this syntax: auto a = ["Hello", "what"]; Gives a fixed-sized array of invariant(char)[]. I think that's asymmetric. My suggestion works with all arrays, so the array 'a' too becomes/is dynamic, keeping the simmetry.
Oct 16 2008
"Andrei Alexandrescu" wroteSteven Schveighoffer wrote:Yes, good point. What about using static, there's a keyword that screams out 'this is static' :) byte[static] = [0,1,2]; or auto[static] = [0,1,2]; // typed as int[3] T[auto] could look like you are trying to declare an AA with the key type being inferred. But of course, that's probably very rare. Just playing around ;) No idea if people would like that syntax, static is already heavily overloaded. -Steve"Sergey Gromov" <snake.scaly gmail.com> wrote in message news:MPG.23615f5e560d4d3f989763 news.digitalmars.com...Walter wanted to do things that way. I wanted to implement things the T[auto] way. The problem with specifying size twice is that there are two points of maintenance. If the language can help, why not? AndreiWed, 15 Oct 2008 21:12:31 -0400, Steven Schveighoffer wrote:Just don't use auto: int[3][3] blah = ... Make it clear your intention. A literal should follow the most common usage, and leave the corner cases to specific syntax. Most people don't use static arrays when working with literals. The most common usage of static arrays I've seen is to establish a buffer on the stack: byte[1000] buf = void;"bearophile" <bearophileHUGS lycos.com> wrote in message news:gd63sv$2j0t$1 digitalmars.com...I can see one need, matrix literals: auto blah = [ [ 1, 0, 0 ], [ 0, 0, -1 ], [ 0, -1, 0 ] ]; so that blah is a sequence of 9 numbers accessed accordingly, not 3 arrays of arbitrary length each. Well, it's probably a special case and deserves a special, safer syntax.Steven Schveighoffer:Sure, make the type dynamic for all array literals. My issue is with your proposal to make the data allocated on the heap. There is no need for an array literal to be typed as a static array. Ever.I think this can be solved even simpler. Make string literal type be invariant(char)[] instead of static array. It does not need to be on the heap. That would solve lots of IFTI problems too.With your idea this syntax: auto a = ["Hello", "what"]; Gives a fixed-sized array of invariant(char)[]. I think that's asymmetric. My suggestion works with all arrays, so the array 'a' too becomes/is dynamic, keeping the simmetry.
Oct 16 2008
Steven Schveighoffer wrote:"Andrei Alexandrescu" wroteT[$] was also suggested. AndreiSteven Schveighoffer wrote:Yes, good point. What about using static, there's a keyword that screams out 'this is static' :) byte[static] = [0,1,2]; or auto[static] = [0,1,2]; // typed as int[3] T[auto] could look like you are trying to declare an AA with the key type being inferred. But of course, that's probably very rare. Just playing around ;) No idea if people would like that syntax, static is already heavily overloaded."Sergey Gromov" <snake.scaly gmail.com> wrote in message news:MPG.23615f5e560d4d3f989763 news.digitalmars.com...Walter wanted to do things that way. I wanted to implement things the T[auto] way. The problem with specifying size twice is that there are two points of maintenance. If the language can help, why not? AndreiWed, 15 Oct 2008 21:12:31 -0400, Steven Schveighoffer wrote:Just don't use auto: int[3][3] blah = ... Make it clear your intention. A literal should follow the most common usage, and leave the corner cases to specific syntax. Most people don't use static arrays when working with literals. The most common usage of static arrays I've seen is to establish a buffer on the stack: byte[1000] buf = void;"bearophile" <bearophileHUGS lycos.com> wrote in message news:gd63sv$2j0t$1 digitalmars.com...I can see one need, matrix literals: auto blah = [ [ 1, 0, 0 ], [ 0, 0, -1 ], [ 0, -1, 0 ] ]; so that blah is a sequence of 9 numbers accessed accordingly, not 3 arrays of arbitrary length each. Well, it's probably a special case and deserves a special, safer syntax.Steven Schveighoffer:Sure, make the type dynamic for all array literals. My issue is with your proposal to make the data allocated on the heap. There is no need for an array literal to be typed as a static array. Ever.I think this can be solved even simpler. Make string literal type be invariant(char)[] instead of static array. It does not need to be on the heap. That would solve lots of IFTI problems too.With your idea this syntax: auto a = ["Hello", "what"]; Gives a fixed-sized array of invariant(char)[]. I think that's asymmetric. My suggestion works with all arrays, so the array 'a' too becomes/is dynamic, keeping the simmetry.
Oct 16 2008
"Andrei Alexandrescu" <SeeWebsiteForEmail erdani.org> wrote in message news:gd7mgi$2f83$1 digitalmars.com...Steven Schveighoffer wrote: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 :) I thought the exact same thing about T[auto], but didn't say anything. It really does make me think "AA of an inferred keytype". Which might not be a bad feature to have, actually. T[$] is too much meaning-overloading. A $ inside of square brackets already means "length". Using it in this way is completely different. Almost looks like a deliberate "buffer overflow"-type ;)"Andrei Alexandrescu" wroteT[$] was also suggested.Steven Schveighoffer wrote:Yes, good point. What about using static, there's a keyword that screams out 'this is static' :) byte[static] = [0,1,2]; or auto[static] = [0,1,2]; // typed as int[3] T[auto] could look like you are trying to declare an AA with the key type being inferred. But of course, that's probably very rare. Just playing around ;) No idea if people would like that syntax, static is already heavily overloaded."Sergey Gromov" <snake.scaly gmail.com> wrote in message news:MPG.23615f5e560d4d3f989763 news.digitalmars.com...Walter wanted to do things that way. I wanted to implement things the T[auto] way. The problem with specifying size twice is that there are two points of maintenance. If the language can help, why not? AndreiWed, 15 Oct 2008 21:12:31 -0400, Steven Schveighoffer wrote:Just don't use auto: int[3][3] blah = ... Make it clear your intention. A literal should follow the most common usage, and leave the corner cases to specific syntax. Most people don't use static arrays when working with literals. The most common usage of static arrays I've seen is to establish a buffer on the stack: byte[1000] buf = void;"bearophile" <bearophileHUGS lycos.com> wrote in message news:gd63sv$2j0t$1 digitalmars.com...I can see one need, matrix literals: auto blah = [ [ 1, 0, 0 ], [ 0, 0, -1 ], [ 0, -1, 0 ] ]; so that blah is a sequence of 9 numbers accessed accordingly, not 3 arrays of arbitrary length each. Well, it's probably a special case and deserves a special, safer syntax.Steven Schveighoffer:Sure, make the type dynamic for all array literals. My issue is with your proposal to make the data allocated on the heap. There is no need for an array literal to be typed as a static array. Ever.I think this can be solved even simpler. Make string literal type be invariant(char)[] instead of static array. It does not need to be on the heap. That would solve lots of IFTI problems too.With your idea this syntax: auto a = ["Hello", "what"]; Gives a fixed-sized array of invariant(char)[]. I think that's asymmetric. My suggestion works with all arrays, so the array 'a' too becomes/is dynamic, keeping the simmetry.
Oct 17 2008
On 2008-10-16 11:27:24 -0400, Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> said:T[$] was also suggested.That looks much better than T[auto]. Because auto usually replaces a type, it makes me think more of an associative array; $ being the length of the array, it is exactaly what I put in the brakets when I want a static array. -- Michel Fortin michel.fortin michelf.com http://michelf.com/
Oct 18 2008
Seeing how very few people care of this topic I presume I'm the only one that does such kind of bugs. So, like the the module system that has several holes, it will not be fixed. Oh well. Bye, bearophile
Oct 16 2008
bearophile Wrote:Seeing how very few people care of this topic I presume I'm the only one that does such kind of bugs. So, like the the module system that has several holes, it will not be fixed. Oh well.http://d.puremagic.com/issues/show_bug.cgi?id=2367#c2
Oct 16 2008
ore-sama:http://d.puremagic.com/issues/show_bug.cgi?id=2367#c2Yes, sorry, I was a little nervous for an unrelated thing :-)Amen to dropping the fixed-sizedness of string literals. I suppose determining the type based on the smallest type that can represent the data without using multibyte encodings is reasonable enough, and you're right, it fits in with the way it works for ints.<My complaint/proposal regards all array literals, to keep the strings more aligned with all the other arrays. I think all array/string literals have to define dynamic arrays by default, and static arrays if the programmer explicitly states so, with some short and handy syntax. I think this is 20-40 times more important than inventing a new syntax that replaces !() Bye, bearophile
Oct 16 2008
bearophile:I think this is 20-40 times more important than inventing a new syntax that replaces !()I find it ironic that 100+ messages are exchanged on this NG about something that 98-99% of people has felt as not broken (the template instantiation syntax) while real problems that I encounter often when I program (array literals, AA literals, etc etc) seem ignored by D developers. This is the biggest difference between an open source language, and one language like D where the inevitably personal bias of 1-3 people guide most of the language design. Bye, bearophile
Oct 16 2008
bearophile wrote:bearophile:I hear you. For the languages I know that has always been the case: one person or small group is behind each language. Except for Ada. What process do you have in mind? I agree that array literals are problematic. I'll about that in another thread. AndreiI think this is 20-40 times more important than inventing a new syntax that replaces !()I find it ironic that 100+ messages are exchanged on this NG about something that 98-99% of people has felt as not broken (the template instantiation syntax) while real problems that I encounter often when I program (array literals, AA literals, etc etc) seem ignored by D developers. This is the biggest difference between an open source language, and one language like D where the inevitably personal bias of 1-3 people guide most of the language design.
Oct 16 2008
Andrei Alexandrescu:I hear you.:-)For the languages I know that has always been the case: one person or small group is behind each language. Except for Ada. What process do you have in mind?- I have seen that to design a language you have to take hundreds or thousands of decisions. Designing languages is hard (and I am a newbie in that job). - Every single person has quirks, idiosyncrasies, etc, this is natural and inevitable. This for example means that everyone has several things that he/she/shi feels as "natural" or right, but they aren't for most other people. - A community usually gives just an average idea; this average is precious because it allows to find what's more natural and intuitive, but once in a while it may lack a coherent style that just 1-3 people may have. So always following such average ideas isn't positive. - So, while designing the feature X in a language in the most rational, intuitive and less error-prone way you have to find a compromise between following a coherent style, and choosing the most commonly appreciated/understood solution. - I have seen Walter and you trying to design or re-design a feature X, where most people here ask to fix the feature Y (and Z) that currently is damning lot of D programmers; while few care of feature X or how it's (re)designed. In more than a year of presence in this forum I think your question to name the "top 5" was one of the few and first times that trend was inverted, asking to the community :-) - Summing it up, I think the current way D is designed isn't the best. I think that its developing process may enjoy listening more to what the average D user wants now (and it seems they often ask for a fix in some of the design mistakes of the language). Sometimes looking for a faster horse is the right thing to do :-)I agree that array literals are problematic. I'll about that in another thread.- Oh, good. Maybe in some time someone will even understand why I think the current module system of D has some holes that it may be positive to fill/fix :-) (I have discussed this topic twice in the past). I think Kirk McDonald is among the few that agrees with me :-) - I think that fixing array literals is much higher priority than "fixing" template instantiation syntax. But on the other hand, from the last discussions it seems that the future D2 language will have a lot of differences from the current D2 language: in practice every 5 posts of yours I see a suggestion (often good!!) to change something quite basic in the way D2 syntax/semantic works :-] - I'm sure your presence in the D community is the best thing happened to D in the last 15 months :-) In truth I have seen people in this community that has ideas often as good as yours regarding the future of D, but you seem more expert. Bye and thank you, bearophile
Oct 16 2008
Steven Schveighoffer wrote:"bearophile" wroteYah Walter does plan to make "string literals" dynamically-sized by default, but fixed-sized on demand (if you specify the appropriate receiver type, which in turn asks the T[auto] question). In related news, he wants to legitimize statically-typed arrays by making them true value types. AndreiBy default arrays/strings in D1 are static: import std.stdio: writefln; void main() { auto a = ["Hello", "what"]; writefln(a[2].length); // 5 } To remove a significant amount of bugs from my code, like that one (the second string is a static array of length 5) I suggest to "invert" the meaning of array literals: by default they define dynamic arrays/strings allocated on the heap (immutable too, if necessary). So a different and explicit syntax can be used/invented to denote static arrays.I think this can be solved even simpler. Make string literal type be invariant(char)[] instead of static array. It does not need to be on the heap. That would solve lots of IFTI problems too.
Oct 16 2008
On Thu, 16 Oct 2008 17:40:23 +0400, Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> wrote:Steven Schveighoffer wrote:Good news!"bearophile" wroteYah Walter does plan to make "string literals" dynamically-sized by default, but fixed-sized on demand (if you specify the appropriate receiver type, which in turn asks the T[auto] question). In related news, he wants to legitimize statically-typed arrays by making them true value types. AndreiBy default arrays/strings in D1 are static: import std.stdio: writefln; void main() { auto a = ["Hello", "what"]; writefln(a[2].length); // 5 } To remove a significant amount of bugs from my code, like that one (the second string is a static array of length 5) I suggest to "invert" the meaning of array literals: by default they define dynamic arrays/strings allocated on the heap (immutable too, if necessary). So a different and explicit syntax can be used/invented to denote static arrays.I think this can be solved even simpler. Make string literal type be invariant(char)[] instead of static array. It does not need to be on the heap. That would solve lots of IFTI problems too.
Oct 16 2008
Andrei Alexandrescu:Yah Walter does plan to make "string literals" dynamically-sized by default, but fixed-sized on demand (if you specify the appropriate receiver type, which in turn asks the T[auto] question). In related news, he wants to legitimize statically-typed arrays by making them true value types.Good. But why not make all array literals dynamically-sized by default , but fixed-sized on demand? Isn't that more symmetric (so the D programmer has to remember less special cased rules == simpler language)? Bye, bearophile
Oct 16 2008
bearophile wrote:Andrei Alexandrescu:I agree. Walter didn't like that though, but I don't remember his argument. I'll defer to him. AndreiYah Walter does plan to make "string literals" dynamically-sized by default, but fixed-sized on demand (if you specify the appropriate receiver type, which in turn asks the T[auto] question). In related news, he wants to legitimize statically-typed arrays by making them true value types.Good. But why not make all array literals dynamically-sized by default , but fixed-sized on demand? Isn't that more symmetric (so the D programmer has to remember less special cased rules == simpler language)?
Oct 16 2008
Andrei Alexandrescu wrote:In related news, he wants to legitimize statically-typed arrays by making them true value types.Huzzah! Sean
Oct 16 2008
Andrei Alexandrescu wrote:In related news, he wants to legitimize statically-typed arrays by making them true value types.YES YEEEEEESSSSSSSS
Oct 16 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: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.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
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:Pad with T.initAs 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?
Oct 18 2008
On 2008-10-18 11:31:09 -0400, KennyTM~ <kennytm gmail.com> said: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/Pad with T.init// 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 } [...]For that to work, you'd also need implcit conversion from T[4] to T[5]. How are your proposing to do that?
Oct 18 2008
Michel Fortin wrote:On 2008-10-18 11:31:09 -0400, KennyTM~ <kennytm gmail.com> said: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".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.Pad with T.init// 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 } [...]For that to work, you'd also need implcit conversion from T[4] to T[5]. How are your proposing to do that?
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...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.Nick Sabalausky: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 }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 18 2008