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

cannot match types if union is behind a type alias #787

Open
Frityet opened this issue Aug 21, 2024 · 3 comments
Open

cannot match types if union is behind a type alias #787

Frityet opened this issue Aug 21, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@Frityet
Copy link

Frityet commented Aug 21, 2024

local type nilable<T> = T | nil
local function divide(a: number, b: number): nilable<number>, nilable<string>
    if b == 0 then
        return nil, "division by zero"
    end
    return a / b, nil
end

local x, err = divide(10, 2)
if err is nil and not x is nil then
    print("Result: "..x)
else
    print("Error: "..err)
end

Results in x being inferred as nil, and err being inferred as nilable<string>. I am using the next branch.

@hishamhm
Copy link
Member

Can you reproduce this with types other than nil? Teal does not support discriminating nil in unions, so you cannot implement nil-strictness by hand, it will need to be added to the language eventually.

@hishamhm
Copy link
Member

I've confirmed this misbehavior with a non-nil-related minimized example

local type Maybe<T> = T | boolean
local x: Maybe<string>
if not x is boolean then
    print("Result: "..x)
end

@hishamhm hishamhm added the bug Something isn't working label Aug 21, 2024
@hishamhm
Copy link
Member

Note to self: the solution to this bug is that union types need to be handled as generic types the same way as functions and records currently are — that is, the type object for the union needs to store type variables, so that resolve_decl_into_nominal can run match_typevals on it correctly — my current plan is to solve this by generalizing all generic types into a single internal $\Lambda$ type, so that we can do beta-reduction at the type level like $(Λα.t^α)(T) \rightarrow t[α := T]$.

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

No branches or pull requests

2 participants