Luau Recap: October 2021

I believe we’ve covered ++ before - we don’t have plans to add it. There’s many reasons as to why
not:

  • Pre- and post- forms are usually considered a mistake in language design, because the return value isn’t immediately obvious to readers unfamiliar with the arcane rules
  • Having an expression that modifies the variable results in complex order dependent evaluation and easy to make mistakes along the lines of process(foo++, foo) – this is also why our compound assignment operators are statements, not expressions
  • Adding a ++ without -- would leave a glaring symmetry gap in the language, but -- can not be added since it already means a comment
7 Likes

I guess that could make sense and honestly slipped over my head there. I come from a C++ background so it’s easy to get used to a certain way of codebase design. But love the work, the if-then-else is something that I instantly knew I’d start using. I’m a person who likes clean, minimal but effective code.

Now I won’t need to do

local value

if condition then value = value1 else value = value2 end

Makes those circumstances much more cleaner and I’m all for it :slight_smile:

1 Like

Curious as to why it wasn’t implemented exactly like Python. I think local a = b if c else d would make much more sense to read out loud, as well as the expression be shorter since there is no need for a then. But regardless, I’ll be using this.

1 Like

Still waiting until Luau’s typechecker supports t[k] on Roblox userdata types and not just table

1 Like

Wait, they just made the Roblox Studio scripting language more fast? Woah

1 Like

The order of evaluation is wrong and parsing it is problematic.

local a = print(1) if print(2) or true else print(3)
-- 2 gets printed first!

local b = print(1) if print(2) or true then print(3) end
-- this is actually 2 statements
local b = print(1)
if print(2) or true then print(3) end
-- figuring out if 'if' is an if statement or if expression is complex
-- with this syntax
3 Likes

Oh ma god. Finally found some lines that I understand lol.

1 Like

So are if-then-else and (x and y or) similar

1 Like

The formatter within the script editor still thinks it’s an if statement without an escape (end) so it moves the next block of code a tab to the right.

2 Likes

I’ve seen a lot of people asking for a different way besides if-then-else, but roblox has many reasons to why this is the case

they have many examples about why if-then-else was chosen via the link

GitHub link

2 Likes

I completely agree. Its an alit better system to use when scripting. This can change a lot in people’s work and most likely shorten the scripts.

2 Likes

Someone else already commented on this above:

The problem is know and planned to be fixed.

3 Likes

Indentation does not happen when the expression is being used to set a variable, however the end autocompletion does:

1 Like
print(if true then false else nil)

image
code works completely fine, no errors in output
just the warn is annoying

i’ve been able to get a lot of warns in the script editor that look a lot like this

--Type 'Type' could not be converted into 'Type'
3 Likes

The indentation and end completion issue in Studio should be fixed now since we enabled support for handling this feature in the editor.

5 Likes

About the typechecker, when can we expect the types returned to be updated? Because currently this will happen:
Screen Shot 2021-11-03 at 8.46.31 AM

I’ve put all kinds of crap into the players service to test this and no matter what it will always only return the player objects.

3 Likes

Roblox types are really poor. I’ve had an extremely consistent experience with Roblox LSP, I’ve never had any issues with type definitions. (Only once with :SetCore)

Also shouldn’t Instance be able to be converted to Player? Is that a strict typing thing?

I wonder if they didn’t make Player the type because it wouldn’t be able to accept children properly. Not sure if Instance even does anyway.

2 Likes

The Roblox typechecker doesn’t automatically replace type’s with with types that inherit from the base, if that’s what you mean. To do that you have to do this:

for _,child : Instance in pairs(instance:GetChildren() do -- Child will default to any, even though :GetChildren only returns instance's. So you have to explicitly define it's type as 'Instance'
	if child:IsA("BasePart") then
		print(child) -- child will now show it's type as 'BasePart' when you hover over it
	end
end

In other words, no, you cannot do this:

for _,child : BasePart in pairs(instance:GetChildren()) do

This is for good reason though, because while BasePart does inherit from Instance, it is only one of the many classes that inherit from it. Because Instance | Roblox Creator Documentation returns all Instance's under the object, not just those with type BasePart, it isn’t compatible.

That isn’t my point however. My point is that Instance:GetChildren returns type {Instance} while Players | Roblox Creator Documentation also returns the type {Instance} even though it should return {Player} since it already filters through the children so that it only returns the children of the type Player.
It explicitly says on the documentation page for Players:GetPlayers: “It functions the same way Instance:GetChildren would except that it only returns Player objects.”

There’s still end completion and indentation issues when using this new syntax. If there is a function in place of either ‘a’ or ‘b’ in if a then b else c it causes issues. Is there any word on if/when we can expect this to be fixed?

2 Likes

Unfreezing a table will never be allowed as it uses the exact same readonly mechanism that the global environment, Instance metatable, and many, many other objects use to protect themselves from malicious modifications. Providing an unfreeze operation would allow these sacred objects to be unprotected.

Plus, the point of freezing is to prevent modification.

3 Likes