From 0843c7219151d0250ba422d912d342e60bf45a39 Mon Sep 17 00:00:00 2001 From: Alice <166900055+alicesaidhi@users.noreply.github.com> Date: Tue, 6 May 2025 21:22:23 +0200 Subject: [PATCH] fix read --- src/read.luau | 43 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/src/read.luau b/src/read.luau index 3764315..857190c 100644 --- a/src/read.luau +++ b/src/read.luau @@ -1,5 +1,42 @@ -local function read(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 -return read +local function read(value: T): CastIntoValue + return if type(value) == "function" then (value :: () -> T)() else value +end + +return read \ No newline at end of file