Array literals' default type
Bill Baxter
wbaxter at gmail.com
Mon Oct 12 17:48:22 PDT 2009
On Mon, Oct 12, 2009 at 5:20 PM, Ellery Newcomer
<ellery-newcomer at utulsa.edu> wrote:
> Yigal Chripun wrote:
>>>
>>> what's wrong with enclosing tuples in parenthesis?
>>> (x, y) = foo();
>
> I'm guessing you'd end up with some weird rules
>
> z = foo(); // what is z, a tuple, or y?
What's weird there? z is whatever type it was declared to be.
> auto (a,b) = ((1,2),(3,4)); // eh, what is this?
That's pretty easy to figure out. Why would it be anything other than
a=(1,2) b=(3,4) ?
> foo((1,2)); // um, what?
You see that kind of thing in Python all the time, with NumPy at
least. Array dimensions for example are set with a tuple. So
x = array((1,2), dtype=int)
And very common to see things like numpy.zeros((10,20)).
> When offered semantic ambiguity, just say no.
You didn't really show any examples of ambiguity that I could see.
Some of those examples may have a legal meaning in C, so that's an
issue if so.
>>>
>>> int foo();
>>> int bar();
>>>
>>> int a = foo(), bar(); // sequence
>>> int b, c;
>>> (b, c) = (foo(), bar()); // tuples
>>> b, c = foo(), bar(); // sequence
>>> (b, c) = foo(), bar(); // error assigning int to (int, int)
>>> b, c = (foo(), bar()); // error assigning (int, int) to int
>>>
>>>
>>
>> one more note:
>>
>> there's no need to special case the for loop.
>>
>> for (int i = 0, j = 0; someCondition; i++, j--) {...}
>> ~~~^~~~~
>> the above will continue to work whether it's a tuple or a C sequence.
>>
>>
>>
>
> How about this?
>
> OtherTypeIwant foo(out bool flag){...}
>
> ...
>
> while(guardflag && (x=foo(f), f)){
> ...
> }
>
>
> Just checked, still have something like this in my code.
That actual code would still be ok because I don't think it wouldn't
be legal to do && on a tuple. Or if so then the result would be a
tuple, and then the while would barf. So it wouldn't compile and
you'd find the porting error.
--bb
More information about the Digitalmars-d
mailing list