mirror of
https://github.com/centau/vide.git
synced 2026-08-20 14:41:37 +00:00
fix read
This commit is contained in:
parent
484a337953
commit
0843c72191
1 changed files with 40 additions and 3 deletions
|
|
@ -1,5 +1,42 @@
|
|||
local function read<T>(value: T | () -> T): T
|
||||
return if type(value) == "function" then value() else value
|
||||
type function CastIntoValue(value: type)
|
||||
|
||||
local function get_return_for_fn(t: type)
|
||||
local values = t:returns()
|
||||
local head = values.head
|
||||
local tail = values.tail
|
||||
|
||||
if head then
|
||||
local first = head[1]
|
||||
return first
|
||||
elseif tail then
|
||||
return tail
|
||||
else
|
||||
return types.singleton(nil)
|
||||
end
|
||||
end
|
||||
|
||||
if value.tag == "union" then
|
||||
local components = value:components()
|
||||
local possible_outcomes = {}
|
||||
|
||||
for _, v in components do
|
||||
if v.tag == "function" then
|
||||
table.insert(possible_outcomes, get_return_for_fn(v))
|
||||
else
|
||||
table.insert(possible_outcomes, v)
|
||||
end
|
||||
end
|
||||
|
||||
return types.unionof(unpack(possible_outcomes))
|
||||
elseif value.tag == "function" then
|
||||
return get_return_for_fn(value)
|
||||
else
|
||||
return value
|
||||
end
|
||||
end
|
||||
|
||||
local function read<T>(value: T): CastIntoValue<T>
|
||||
return if type(value) == "function" then (value :: () -> T)() else value
|
||||
end
|
||||
|
||||
return read
|
||||
Loading…
Add table
Add a link
Reference in a new issue