Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow parentheses to be omitted around tuples where sensible #210

Open
SheepTester opened this issue Jul 5, 2021 · 5 comments
Open

Allow parentheses to be omitted around tuples where sensible #210

SheepTester opened this issue Jul 5, 2021 · 5 comments
Labels
enhancement New feature or request python Involves the Python implementation
Milestone

Comments

@SheepTester
Copy link
Member

In Python, you don't need parentheses around tuples all the time. For example,

# Swaps variables
a, b = b, a

# Iterate over list with index
for i, item in enumerate(list_of_items_):

# Return a tuple
return None, False

Currently, N requires parentheses around tuple expressions/literals and types (but not patterns, I think). So, currently, the programmer has to write

let a, b: (char, float) = (\{π}, 3.14)

I feel like those parentheses around the value and type annotation aren't necessary, so instead, one can just write

let a, b: char, float = \{π}, 3.14

Parentheses will still be required inside type variables, lists, function arguments, and other tuples:

let wow: list[(char, float)] = [(\{e}, 2.72), (\{π}, 3.14)]

print((1, (2, 3)))

so I don't think allowing their omission should cause any problems.

(I'm adding the Python label because it's not in the Python implementation but it is in the JS implementation as of now; if this feature request gets rejected then I'll have to fix the JS impl.)

@SheepTester SheepTester added enhancement New feature or request python Involves the Python implementation labels Jul 5, 2021
@SheepTester
Copy link
Member Author

To implement this, I think we can first rename expression to nontuple_expression, and then replace the mentions of expression in listval, etc. with nontuple_expression. Then, we can define expression to be either a nontuple_expression or a tupleval. We can then remove parentheses from tupleval. Because expression can also be boolean_expression, we probably should either require tupleval to have at least two items (changing * -> + should do that) or in scope.py check how many items there are before creating a tuple.

tupleval: nontuple_expression ("," nontuple_expression)+ (",")?
?nontuple_expression: ifelse_expr
           | boolean_expression
           | function_def
           | anonymous_func
           | function_callback_pipe
           | match
?expression: nontuple_expression
           | tupleval

And something similar for tuple types (tupledef).

We'd also have to relax the use of parentheses around tuples in the tests

@Ashvin-Ranjan Ashvin-Ranjan added this to the N 1.3.0 milestone Jul 5, 2021
@SheepTester
Copy link
Member Author

Also, just to be safe, we should disallow a trailing comma unless it's inside parentheses

@Ashvin-Ranjan
Copy link
Contributor

I think this is not that worth it for the number of edge cases it adds.

@SheepTester
Copy link
Member Author

edge cases

Do you have any examples?

@SheepTester SheepTester modified the milestones: N 1.3.0, N Next Jul 9, 2021
@Ashvin-Ranjan
Copy link
Contributor

I still dislike this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request python Involves the Python implementation
Projects
None yet
Development

No branches or pull requests

2 participants