luau/docs/_posts/2022-06-01-luau-recap-may-2022.md
Vyacheslav Egorov b56b2a71b8 Recap is ready
2022-06-01 16:45:42 +03:00

3.9 KiB

layout title
single Luau Recap: May 2022

Read more about Luau at https://luau-lang.org.

[Cross-posted to the Roblox Developer Forum.]

This month Luau team has worked to bring you a new language feature together with more typechecking improvements and bugfixes!

Generalized iteration

We have extended the semantics of standard Lua syntax for iterating through containers, for vars in values with support for generalized iteration. In Lua, to iterate over a table you need to use an iterator like next or a function that returns one like pairs or ipairs. In Luau, you can now simply iterate over a table:

for k, v in {1, 4, 9} do
    assert(k * k == v)
end

This works for tables but can also be customized for tables or userdata by implementing __iter metamethod. It is called before the iteration begins, and should return an iterator function like next (or a custom one):

local obj = { items = {1, 4, 9} }
setmetatable(obj, { __iter = function(o) return next, o.items end })

for k, v in obj do
    assert(k * k == v)
end

The default iteration order for tables is specified to be consecutive for elements 1..#t and unordered after that, visiting every element. Similar to iteration using pairs, modifying the table entries for keys other than the current one results in unspecified behavior.

Typechecking improvements

We have added a missing check to compare implicit table keys against the key type of the table indexer:

-- error is correctly reported, implicit keys (1,2,3) are not compatible with [string]
local t: { [string]: boolean } = { true, true, false }

Rules for == and ~= have been relaxed for union types, if any of the union parts can be compared, operation succeeds:

--!strict
local function compare(v1: Vector3, v2: Vector3?)
    return v1 == v2 -- no longer an error
end

Table value type propagation now correctly works with [any] key type:

--!strict
type X = {[any]: string | boolean}
local x: X = { key = "str" } -- no longer gives an incorrect error

If a generic function doesn't provide type annotations for all arguments and the return value, additional generic type parameters might be added automatically:

-- previously it was foo<T>, now it's foo<T, b>, because second argument is also generic
function foo<T>(x: T, y) end

We have also fixed various issues that have caused crashes, with many of them coming from your bug reports.

Autocomplete improvements

Studio autocomplete has received a few improvements:

  • Function argument names have been added to the function signature display
  • Instance:GetAttribute and Instance:SetAttribute will now suggest attribute names if instance can be resolved
  • Inside the new if-then-else expression, incorrect suggestions following else have been fixed
  • Deprecated lowercase Color3:toHSV so it's no longer suggested
  • Deprecated lowercase Instance:findFirstChild, Instance:clone, Instance:remove, Instance:getChildren and old alias called Instance:children.

Linter improvements

global-used-as-local lint warning has been extended to notice when global variable writes always happen before their use in a local scope, suggesting that they can be replaced with a local variable:

function bar()
    foo = 6 -- Global 'foo' is never read before being written. Consider changing it to local
    return foo
end
function baz()
    foo = 10
    return foo
end

Performance improvements

Garbage collection CPU utilization has been tuned to further reduce frame time spikes of individual collection steps and to bring different GC stages to the same level of CPU utilization.

In open-source release of Luau, when optimization level 2 is enabled, compiler will now perform function inlining. Loop unrolling in optimization level 2 has also been improved to handle all language constructs inside the loop body.