Luau Recap: March 2023

That’s strange:

1 Like

Oh the post meant inside the ‘^’ I thought it was something else big misunderstand.

1 Like

It seems that type checking isn’t working for metamethods like __mul; it insists that the return type must be the same as the metatable type;

It insists that op1 and op2 and the return type must all be Quaternions, some common counter examples to this is the fact you can multiply Vector3’s and numbers, CFrames and Vector3’s, etc…

--!strict
export type Vec2 = typeof(setmetatable({}, {})) & {
	X: number,
	Y: number,
}

local Vec2 = {__type = "Vec2"}
Vec2.__index = Vec2

local function getType(obj: any): string
	if obj ~= nil then
		local objMetatable = getmetatable(obj)
		if type(objMetatable) == "table" and objMetatable.__type ~= nil then
			if typeof(objMetatable.__type) == "string" then
				return objMetatable.__type
			end
			return "nil"
		else
			return typeof(obj)
		end
	end
	return "nil"
end

function Vec2.new(X: number?, Y: number?)
	local tX, tY = typeof(X), typeof(Y)
	if tX ~= "number" and tX ~= "nil" then error("") end
	if tY ~= "number" and tY ~= "nil" then error("") end
	
	return setmetatable({
		X = X,
		Y = Y
	}, Vec2)
end

function Vec2.__mul(op1: Vec2 | Vector2 | number, op2: Vec2 | Vector2 | number): Vec2 | Vector2
	local op1t, op2t = getType(op1), getType(op2)
	if op1t == "Vec2" and op2t == "number" then
		return Vec2.new(op1.X * op2, op1.Y * op2)
	end
	return Vec2.new()
end

This is some short reproduction code.

--!strict
-- Ensure workspace is Workspace and not any
local workspace = workspace::Workspace

local foo:{[Part]: boolean} = {}
local bar = workspace:FindFirstChild("Part")
if foo[bar] then
	-- bar: Type Instance could not be converted into Part
	-- I expected bar's type to be inferred as Part, because this block
	-- can only execute if bar is a Part.
	-- Instead, the type checker insists that I am not indexing the table correctly
end

new to luaU types, cant seem to assign this nested table a type

type array = {any}

local yeh: array = {} -- 1
NetworkingService.PlayerData: array = {} -- 2

1 works fine, but 2 errors with “Syntax Error: (17,1) Expected ‘(’, ‘{’ or when parsing function call, got ‘=’”

I believe it needs to be created when defining the table.

We do not plan to make return optional at the end of functions returning Type?.
There’s a runtime difference between returning nil and returning nothing.

For continue and break I hope that we’ll make that improvement, we recently received an open-source contribution that implements that and will be looking at how good it is.

There are no plans for error suppression inside pcall, I don’t remember this being requested before.

FindFirstChild can technically fail to find a child, that’s why it’s an Instance?.
We talked before about special access operator that allows you to ignore nil to help navigation, but it will not be implemented: RFC: Do not implement non-nil postfix `!` operator. by alexmccord · Pull Request #420 · Roblox/luau · GitHub

I’m not sure that all of your complaints work as bug reports, some sound more like feature requests.

1 Like

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.