4 ms·
No, it’s actually because , (comma) denotes a tuple. [1, 2; 3, 4] is a list of tuples, of type (int * int) list, equivalent to [(1, 2); (3, 4)]
by tomp 4y ago
No, it’s actually because , (comma) denotes a tuple.
[1, 2; 3, 4]
is a list of tuples, of type (int * int) list, equivalent to
[(1, 2); (3, 4)]
- Athas 4y agoI never understood the motivation for allowing tuples without surrounding parens. There are a few very simple functions that look cuter this way, but it constantly trips me up in more complex situations.
- a-nikolaev 4y agoNo parentheses can be nice for cleaner pattern matching, e.g. something like this: match a, b with | 1, 2 -> x | _ -> y
- cwzwarich 4y agoThis phenomenon is actually another example of the phenomenon he mentions. In strict precedence parsing, operators are assigned meaning rather than productions of a grammar (which would could those operators in distinct contexts), so once you've decided a comma indicates a tuple, it's easier to just allow this syntax.
- goodoldneon 4y agoThe "comma makes a tuple" behavior is a big gotcha I've experienced in Python. I wish that behavior didn't exist
- LynxInLA 4y agoWhich version of that are you talking about in Python? The biggest related gotcha I've seen in Python is when you have a trailing comma `x = 1,` and accidentally create a tuple.
- goodoldneon 4y agoYea that’s the most common gotcha