-
Notifications
You must be signed in to change notification settings - Fork 57
RFC: if
and while
statement initializers (if local
and while local
statements)
#110
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
base: master
Are you sure you want to change the base?
Conversation
…ajor verbiage changes to clarify and improve arguments
The Perhaps a semicolon is a more natural statement separator? And perhaps that leads to the notion of those if
name ~= nil;
local player = Players:FindFirstChild(name);
local character = player.Character;
local humanoid = character:FindFirstChildOfClass("Humanoid");
humanoid.Health > 0
then
humanoid.Health -= 5;
end A simpler example: if local match = s:match(REGEX); #match > 2 then
print("found it!")
end I think that, while I appreciate the attempt to use a keyword in line with the rest of Luau, that it's better to use a symbol that people can assign meaning to themselves, rather than constructing an explicit phrase that reads wonkily. |
Hmm, Otherwise, I kinda like the idea that you can mix regular For example: if
name ~= nil; -- we don't even need this one tbh
local player = Players:FindFirstChild(name)
local character = player.Character
local humanoid = character:FindFirstChildOfClass("Humanoid");
humanoid.Health > 0
then
humanoid.Health -= 5
end |
We could adopt the same tack as semicolons in normal Luau; can be used to disambiguate statements, but when there's no ambiguity perhaps there's no need for them. |
I'm reminded of a humorous idea from the previous RFC's comments.. if
local character = hit.Parent if character:IsA("Model")
local hit_player = Players:GetPlayerFromCharacter(model) if hit_player.Team ~= player.Team
local humanoid = character:FindFirstChildOfClass("Humanoid") if humanoid.Health > 0
then
humanoid.Health = math.max(0, humanoid.Health - 5)
end
Nevermind, it's not parseable without parens. Forgot about |
I was going to suggest One thing I'm concerned about wrt data = "hello" in fetch(data) if local data = "hello" in fetch(data) then
...
end And the other usage of -- Data flow -> -> ->
if local data = "hello" in fetch(data) then
-- Data flow <- <- <-
for key, value in contents do Nor would any future prospective use of -- Data flow <- <- <-
local { useState, useMemo } in require("react") local data = { 2, 4, 6, 8 }
-- Data flow <- <- <-
local indexOf = 6 in data
if indexOf ~= nil then
...
end Since we're locked into using reserved keywords or symbols for disambiguity, and we're looking for a statement/expression terminator, I'd rather we stick to something that already matches that function. Semicolons seem to fit that bill, which is why I would prefer those. |
That's a really good point on the information flow direction; I was somewhat worried about the same kind of thing when I first saw let () =
let a = 1 in
let b = a + 1 in
print_endline(string_of_int b) And I'd say Luau programmers would be a lot more familiar with Python, Lua, JavaScript, (and for compiled languages, probably Rust, C/++, etc.) over OCaml, and so For what it's worth, we aren't actually locked into existing keywords here; in fact, the previous rfc originally suggested a new conditional keyword Although I like the idea of making the evaluation condition non-special, I'm wondering how that could affect its implementation. From what I know from last time I talked to Luau engineers about this, it was reasonable to special case -- using ; instead of in for ya
if
local character = hit.Parent; character:IsA("Model")
local hit_player = Players:GetPlayerFromCharacter(model); hit_player.Team ~= player.Team
local humanoid = character:FindFirstChildOfClass("Humanoid"); humanoid.Health > 0
then
humanoid.Health = math.max(0, humanoid.Health - 5)
end Could expand to if local character = hit.Parent; character:IsA("Model") then
if local hit_player = Players:GetPlayerFromCharacter(model); hit_player.Team ~= player.Team then
if local humanoid = character:FindFirstChildOfClass("Humanoid"); humanoid.Health > 0 then
humanoid.Health = math.max(0, humanoid.Health - 5)
end
end
end if it's not possible to get a more efficient/bespoke pancake implementation. Also just looking at the syntax highlighting for the above two.. I kinda want my red |
I really don't like that it does a nilness check not a truth check as the primary condition. Semantics for if local success, result = pcall(foo) then
if success then example demonstrates confusion as one might expect the +1 on the dislike of |
This RFC is an update/continuation/alternative to if statement initializers (#23), with updated fallthrough semantics, the addition of
while local
statements, and pancakes (stackedlocal
initializations inif local
statements).Rendered.
Basically,
in Roblox examples:
and as a better way of handling nested
if local
s,This RFC turned out quite long, apologies, but the discussion on if statement initializers was pretty long and there's a lot to keep track of and address, especially in terms of alternatives. Please enjoy!