June 14, 2004 Re: Array literals? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russ Lewis | Sounds a bit complicated to me, too. How about the safer alternative:
* In an array literal, all the elements have to have the same type without any implicit conversions. Mixing types is an error.
* Afterwards, there implicit conversion happens on array type in the same
way as it happens for simple types:
int[3] is implicitely converted to real[3] etc.
For multidimensional array literals, it is just the same: All the elements have to have the same type (and dimension) if you put them into a higher dimensional array literal.
Russ Lewis wrote:
> Walter wrote:
>>> void anotherfunc(float a);
>>> . . .
>>>
>>> anotherfunc(4); // '4' is a float, no?
>>
>>
>> No, '4' is an int. It gets implicitly converted to a float by matching it against the argument of anotherfunc. Importantly, the types are determined *before* overload function matching is done, otherwise it could never figure out which function to call.
>
> How about these rules:
>
> 1) Initially, all values in the array are assigned their types using the
> same rules as ordinary constants. So the array literal
> [1,1.0,1]
> starts with the types
> [int,float,int]
>
> 2) The various values are implicitly converted to the least complex common type. In the example above, the values are converted to [float,float,float]
>
> 3) Once all of the values have the same type, the type of the array is
> known:
> float[3]
>
> 4) If the array constant is assigned to some other array, then array
> types are automatically converted up, just like the values would have
> individually:
> double[] var = [1,1.0,1];
> The float[3] array constant is converted up to double[3].
>
> 5) The process proceeds recursively for multidimensional arrays:
> [[1,1.0,1],[2,3,4],[5,6,7]]
> [[int,float,int],[int,int,int],[int,int,int]]
> [[float,float,float],int[3],int[3]]
> [float[3],int[3],int[3]]
> [float[3],float[3],float[3]]
> float[3][3]
|
June 14, 2004 Re: Array literals? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sam McCall | Sam McCall wrote:
> Carlos Santander B. wrote:
>
>> I'd prefer it as C# does it: new int[] [0,1,2,3];
>
> Or new int[0,1,2,3]?
> Sam
Oops, this would be a rectangular array, wouldn't it...
Ignore me ;-)
Sam
|
June 14, 2004 Re: Array literals? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Carlos Santander B. | "Carlos Santander B." <carlos8294@msn.com> wrote in message news:cairv5$1l38$1@digitaldaemon.com... > "Matthew" <admin@stlsoft.dot.dot.dot.dot.org> escribió en el mensaje > news:caimnk$1e7l$1@digitaldaemon.com > | Why not require some kind of syntactic hint, as in: > | > | func([cast(int)0, 1, 2, 3]); > | > | or > | func([int: 0, 1, 2, 3]); > | > | or > | > | func((int[])[0, 1, 2, 3]); > | > | or > | > | func(int[0, 1, 2, 3]); > | > | ? > | > > I'd prefer it as C# does it: new int[] [0,1,2,3]; Or what about new int[] = [1,2,3]; new int[2,2] = [[1,2],[3,4]] ... > > ----------------------- > Carlos Santander Bernal > > |
June 14, 2004 Re: Array literals? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ivan Senji | On Mon, 14 Jun 2004 13:13:47 +0200, Ivan Senji wrote: > "Carlos Santander B." <carlos8294@msn.com> wrote in message news:cairv5$1l38$1@digitaldaemon.com... >> "Matthew" <admin@stlsoft.dot.dot.dot.dot.org> escribió en el mensaje news:caimnk$1e7l$1@digitaldaemon.com >>| Why not require some kind of syntactic hint, as in: >>| >>| func([cast(int)0, 1, 2, 3]); >>| >>| or >>| func([int: 0, 1, 2, 3]); >>| >>| or >>| >>| func((int[])[0, 1, 2, 3]); >>| >>| or >>| >>| func(int[0, 1, 2, 3]); >>| >>| ? >>| >> >> I'd prefer it as C# does it: new int[] [0,1,2,3]; > > Or what about > new int[] = [1,2,3]; > new int[2,2] = [[1,2],[3,4]] > ... > Actually, now I've thought about this a bit more, I'll stick to my usual practice of not using anonymous literals. I like to do it this way instead... private static int[] InitWhatever = [1,2,3]; . . . func(InitWhatEver); As this is a bit more self-documenting. -- Derek Melbourne, Australia |
June 14, 2004 Re: Array literals? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Derek | "Derek" <derek@psyc.ward> wrote in message news:157bpfcf3qs7n.lj00f9w3ri2m$.dlg@40tude.net... > On Mon, 14 Jun 2004 13:13:47 +0200, Ivan Senji wrote: > > > "Carlos Santander B." <carlos8294@msn.com> wrote in message news:cairv5$1l38$1@digitaldaemon.com... > >> "Matthew" <admin@stlsoft.dot.dot.dot.dot.org> escribió en el mensaje news:caimnk$1e7l$1@digitaldaemon.com > >>| Why not require some kind of syntactic hint, as in: > >>| > >>| func([cast(int)0, 1, 2, 3]); > >>| > >>| or > >>| func([int: 0, 1, 2, 3]); > >>| > >>| or > >>| > >>| func((int[])[0, 1, 2, 3]); > >>| > >>| or > >>| > >>| func(int[0, 1, 2, 3]); > >>| > >>| ? > >>| > >> > >> I'd prefer it as C# does it: new int[] [0,1,2,3]; > > > > Or what about > > new int[] = [1,2,3]; > > new int[2,2] = [[1,2],[3,4]] > > ... > > > > Actually, now I've thought about this a bit more, I'll stick to my usual practice of not using anonymous literals. I like to do it this way instead... > > private static int[] InitWhatever = [1,2,3]; > . . . > func(InitWhatEver); > > As this is a bit more self-documenting. Yes it is, but it is useless as it is now! You can't do something like: int[] elems = [1,2,x2-x1]; Once i had a matrix class and it worked like this: i create a static identity matrix: static int[3][3] idn = [[1,0,0],[0,1,0],[0,0,1]]; then i passed this to matrix constructor, the constructor created a copy of it, then i called a member function that changed every element i needed to change! And it is a lot of work, when i think i could (with array litterals) do this: Mat!(3) M1 = new Mat!(3)( new float[3][3]=[[1,0,0],[0,1,0],[x2-x1,y2-y1,1]] ); This way my constructor wouldn't even have to create a deep copy but just copy a reference! An the line above is much more self-documenting then what i have now: static float[3][3] idn = [[1,0,0],[0,1,0],[0,0,1]]; Mat!(3) M1 = new Mat!(3)(idn); //creates a deep copy of idn M1.set(0,2,x2-x1); //did i get the indexes right? M1.set(1,2,y2-y1); > -- > Derek > Melbourne, Australia |
June 14, 2004 Re: Array literals? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ivan Senji | Ivan Senji wrote: > "Carlos Santander B." <carlos8294@msn.com> wrote in message > news:cairv5$1l38$1@digitaldaemon.com... <snip> >> I'd prefer it as C# does it: new int[] [0,1,2,3]; Just as ambiguous: an array of length 0,1,2,3 of arrays of ints? > Or what about > new int[] = [1,2,3]; > new int[2,2] = [[1,2],[3,4]] > ... Good idea I reckon. Since IINM a NewExpression isn't a lvalue semantically, I guess it needn't be one syntactically either, IWC this syntax would fit in. 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. |
June 14, 2004 Re: Array literals? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | In article <cajaj5$2e27$1@digitaldaemon.com>, Walter says... > > >"Regan Heath" <regan@netwin.co.nz> wrote in message news:opr9kb88nu5a2sq9@digitalmars.com... [...] >> True, ok, how bout: >> >> func( cast(int[]) [0,1,2,3] ); >> >> ? > >Ugly <g>. and: func ( int[] : [0, 1 , 2, 3] ); ? Ciao P.S.: what about ':' being accepted in type declarations: int : var1, var2; float[] : var3; int[][char] : var4; It seems clearer, and it can be optional, too. |
June 14, 2004 Re: Array literals? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ivan Senji | On Mon, 14 Jun 2004 14:24:05 +0200, Ivan Senji wrote: > "Derek" <derek@psyc.ward> wrote in message news:157bpfcf3qs7n.lj00f9w3ri2m$.dlg@40tude.net... >> On Mon, 14 Jun 2004 13:13:47 +0200, Ivan Senji wrote: >> >>> "Carlos Santander B." <carlos8294@msn.com> wrote in message news:cairv5$1l38$1@digitaldaemon.com... >>>> "Matthew" <admin@stlsoft.dot.dot.dot.dot.org> escribió en el mensaje news:caimnk$1e7l$1@digitaldaemon.com >>>>| Why not require some kind of syntactic hint, as in: >>>>| >>>>| func([cast(int)0, 1, 2, 3]); >>>>| >>>>| or >>>>| func([int: 0, 1, 2, 3]); >>>>| >>>>| or >>>>| >>>>| func((int[])[0, 1, 2, 3]); >>>>| >>>>| or >>>>| >>>>| func(int[0, 1, 2, 3]); >>>>| >>>>| ? >>>>| >>>> >>>> I'd prefer it as C# does it: new int[] [0,1,2,3]; >>> >>> Or what about >>> new int[] = [1,2,3]; >>> new int[2,2] = [[1,2],[3,4]] >>> ... >>> >> >> Actually, now I've thought about this a bit more, I'll stick to my usual practice of not using anonymous literals. I like to do it this way instead... >> >> private static int[] InitWhatever = [1,2,3]; >> . . . >> func(InitWhatEver); >> >> As this is a bit more self-documenting. > > Yes it is, but it is useless as it is now! > You can't do something like: > > int[] elems = [1,2,x2-x1]; I'm sorry. I misunderstood. I was just thinking of literals as values that could be determined at compile-time and didn't consider run-time values. -- Derek Melbourne, Australia |
June 14, 2004 Re: Array literals? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matthew | Matthew wrote:
> Sounds clever, to be sure, but I fear it'd be a source of huge hidden bugs.
> Imagine the horror that could spawn from a simple typo involving "." rather than
> ","
When I first read this post, I totally agreed with you that this was a problem. Then I remembered that the exact same problem exists with array initializers:
float[] foo = [ 1,2 , 3.4 ];
// oops, meant [ 1.2 , 3.4 ]
So I would argue that if it's good enough for initializers then it's probably good enough for literals :)
|
June 14, 2004 Re: Array literals? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | Walter wrote: > "Matthew" <admin@stlsoft.dot.dot.dot.dot.org> wrote in message > news:cajh6n$2pdn$1@digitaldaemon.com... > >>Sounds clever, to be sure, but I fear it'd be a source of huge hidden > > bugs. > >>Imagine the horror that could spawn from a simple typo involving "." > > rather than > >>"," > > > I agree. I'd rather have a simple and easy to understand rule that is > perhaps a bit klunky rather than a complex, powerful rule that few > understand (like C++ overloading rules <g>). Please interpret me as quizzical, not flaming here: I don't understand what's confusing about what I proposed. It seemed really straightforward. I'm willing to accept it if people find it confusing, but I would like if people could explain why... > It's also important, in language design, to have a bit of redundancy in the > syntax. Redundancy is what makes it possible for a compiler to detect errors > and issue a semi-comprehensible error message. If a language had no > redundancy, then every random string of bytes is a valid program. Good point. I'm sort of liking the suggestions that are something like float[] [ 1, 2.3, 4 ] both because a) They are non-ambiguous b) They look a lot like delegate literals. :) |
Copyright © 1999-2021 by the D Language Foundation