-
Notifications
You must be signed in to change notification settings - Fork 51
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
Pattern and match expression syntax #100
base: master
Are you sure you want to change the base?
Pattern and match expression syntax #100
Conversation
I believe that Luau could benefit more from a native implementation of complex Regular Expressions, either with a special literal like |
@Wunder-Wulfe |
@Wunder-Wulfe I can't think of a syntax for it right now but if the following was a string pattern (instead of an exact): "%d+" It would be equivalent to |
I generally like the semantics proposed, however I'm not quite convinced on the proposed syntax and the value-add for Luau. In general, I think the RFC needs to more clearly explain why Syntax-wise, No parens We don't require parentheses/punctuation anywhere else to denote the start/end of blocks/exprs/control flow, so we shouldn't here.
I don't think
I think the catch-all case should be a required trailing
At the risk of provoking a bikeshed, I would prefer Proposal: Putting the With that out of the way, here's what I think match expressions could look like: local function parse_simple_expr(): AstExprNode
return match current_token.kind in
"string" => parse_string_expr(),
"number" => parse_number_expr(),
"true" or "false" => parse_boolean_expr(),
else error(`unexpected token {current_token.kind} "{current_token.text}" {current_token.span.x}:{current_token.span.y}`)
end I feel this looks a lot cleaner, more readable, and more consistent with the rest of Luau. But you can also clean it up with a pretty-readable if-expression as well: local function parse_simple_expr(): AstExprNode
local kind = current_token.kind
return
if kind == "string" then
parse_string_expr()
elseif kind == "number" then
parse_number_expr()
elseif (kind == "true" or kind == "false") then
parse_boolean_expr()
else
error(`unexpected token {kind} "{current_token.text}" {current_token.span.x}:{current_token.span.y}`)
end Value proposition In Rust, match expressions are extremely powerful as they're used to destructure enums, introduce bindings, cleanly handle In Luau, I feel like the best usecase for match expressions would be in ergonomic table destructuring -- without which match expressions might not provide much more value than if-else expressions (except for decreased verbosity). Anyway, good effort on the RFC. I really like Rust's match expressions and I do miss them in Luau. I just want to make this RFC a bit stronger! Please let me know your thoughts on these proposed changes. |
This is my first time contributing to the language and RFC process, if there are any mistakes or I've missed something please let me know.
Rendered