ideawu / cpy

Cpy provides you a way to write Python codes in C syntax!
62 stars 19 forks source link

Assigning from tuples #2

Open mklingen opened 9 years ago

mklingen commented 9 years ago

The following expression does not work:

function f(a, b)
{
    return (a + b, a * b);
}

print f(1, 2);

It gives the error:

line 3:17 mismatched input u',' expecting ')'
line 3:21 no viable alternative at input u'*'
line 3:24 extraneous input u')' expecting ';'

Similarly, this:

x = (1, 2);

gives the error:

line 1:6 missing ')' at u','
line 1:8 no viable alternative at input u'2'

What's the proper way of dealing with tuples in cpy?

ideawu commented 9 years ago

Hi, cpy does not support tuples grammar, you can use the tuple function to convert list to tuple:

c = 2;
d = 3;
a = tuple([1+c, 2+d]);
print a;
mklingen commented 9 years ago

That's too bad, because it prevents cpy from being able to interface with libraries that return multiple values. Consider a library that returns a tuple. In python, it would be as easy as:

(return_1, return_2) = library.function()

In cpy, would we have to do this?

return_tuple = library.function();
return_1 = return_tuple[0];
return_2 = return_tuple[1];

If so, that's fine, but maybe it should be in the documentation.