Skip to content

Commit

Permalink
make function types as constraints usable
Browse files Browse the repository at this point in the history
See #211.
  • Loading branch information
hishamhm committed Jan 3, 2025
1 parent 8d9a26f commit 2fd5e7e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
11 changes: 10 additions & 1 deletion spec/lang/declaration/local_function_spec.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
local tl = require("tl")
local util = require("spec.util")

describe("local function", function()
Expand All @@ -21,6 +20,16 @@ describe("local function", function()
local ok = f(3, "abc")
]]))

it("can have type variables with constraints", util.check([[
local type F = function(any...): any...
local function partial<T is F>(f: T, a: any): T
return function(...: any): any...
return f(a, ...)
end
end
]]))

it("cannot have unused type variables", util.check_type_error([[
local function f<Z>(a: number, b: string): ()
return
Expand Down
5 changes: 4 additions & 1 deletion tl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8408,8 +8408,11 @@ do

function TypeChecker:to_structural(t)
assert(not (t.typename == "tuple"))
if t.typename == "typevar" and t.constraint then
t = t.constraint
end
if t.typename == "nominal" then
return self:resolve_nominal(t)
t = self:resolve_nominal(t)
end
return t
end
Expand Down
5 changes: 4 additions & 1 deletion tl.tl
Original file line number Diff line number Diff line change
Expand Up @@ -8408,8 +8408,11 @@ do

function TypeChecker:to_structural(t: Type): Type
assert(not t is TupleType)
if t is TypeVarType and t.constraint then
t = t.constraint
end
if t is NominalType then
return self:resolve_nominal(t)
t = self:resolve_nominal(t)
end
return t
end
Expand Down

0 comments on commit 2fd5e7e

Please sign in to comment.