Skip to content
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

lost type with or operator #706

Closed
fperrad opened this issue Oct 17, 2023 · 0 comments · Fixed by #709
Closed

lost type with or operator #706

fperrad opened this issue Oct 17, 2023 · 0 comments · Fixed by #709
Labels
bug Something isn't working

Comments

@fperrad
Copy link
Contributor

fperrad commented Oct 17, 2023

the or operator allows some idioms for default value.

local t1: {string:number} = { foo = 42 }
for _, v in pairs(t1) do
    print(v * v)
end
local t2: {string:number} = { foo = 42 }
for _, v in pairs(t2 or {}) do
    print(v * v)
end
for _, v in pairs(t1 or t2) do
    print(v * v)  -- ERROR: cannot use operator '*' for types B (unresolved generic) and B (unresolved generic)
end
for _, v in pairs((t1 or t2) as {string:number}) do  -- workaround with a cast
    print(v * v)
end

local function foo(a: number)
    a = a or 1  -- the most known idiom
    print(a * a)
end

foo(2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants