-
-
Notifications
You must be signed in to change notification settings - Fork 120
-
-
Notifications
You must be signed in to change notification settings - Fork 120
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
Extend assignment function notation / make more things expressions #190
Comments
Also, as a precondition for this syntax, assignment functions would need to be changed to return the last expression in the chain of execution rather than the expression on the last line of the function. For example:
I think this is desirable on its own as it simplifies the use assignment functions. There'd be no need to create a temporary variable to hold a return value that's subsequently placed on the last line of the function, and no need for creating explicit early "return" statements either. |
As per Gitter discussion, implementation of this should involve having |
I think the danger of this is that it makes assignment functions much less clear in terms of what they're returning—if you want to return something from a nested block, I think it's a good idea to just force an explicit |
This is really a subcase of everything-is-an-expression, isn't it? That's why it works in the languages people are coming from (ML-based, Rust, Haskell, ...) |
@akdor1154 Allowing almost everything as an expression actually would likely be possible without breaking the rule that all valid Python 3 is valid Coconut, but I'm not convinced that there's actually enough of a use case for it to justify implementing it. In particular, I think statement lambdas (especially pattern-matching statement lambdas) already cover most of the use cases for something like that. |
Its not so much about filling use case checkboxes, it's more (again, just from user perspective here) about consistency. There are already special cases that work towards this: your alternative ternary operator (not needed if With everything-is-expression, the above special cases mostly become unnecessary, and the requests for other special cases (why not a match expr to go with ternary expr? Why does a statement lambda on my last line return a func, but a def func return None?) go away. As before, the languages Coconut is inspired by largely work this way already - as a user I find the python 'actually this thing is a statement and will silently eval to None' paradigm to be jarring in Coconut's otherwise functional environments. In practice with these other languages I haven't noticed an issue with clarity around what a deeply nested expression returns - as long as there is clear syntax to delineate the expression as a unit, which Py/Coconut's whitespace does beautifully. |
Honestly, there are a bunch of patterns where this would be useful. Getting rid of multiple return statements reduces visual clutter and lets me focus on what I actually want to read. This is important for accessibility – personally I have to use giant fonts for vision reasons. Having more expressions available also lends itself to a data flow oriented programming style, which is why I would like to use coconut in the first place. The way expressions vs statements are handled in coconut right now feels a bit out of place for a language that aims to facilitate a functional programming style. I agree, that the syntax you get from allowing statements as expressions without any extra syntax elements, a bit awkward:
Maybe enforcing a syntax with round braces would be better:
Or with a special keyword:
|
I love how |
I'm not currently sold on this, but I certainly see the appeal. Here are what I see as the major hurdles to something like this:
I'm open to suggestions if anyone has ideas for this that would rectify the two issues above. |
I think it would be useful if "match" and "case" statements were actually expressions. For example, I'd like to be able to do things like:
The text was updated successfully, but these errors were encountered: