Redesign Result type for cleaner Luau inference#1
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Ok<T>andErr<E>into a singleResult<T, E>shape that carries both_value: Tand_err: Eon the same record (instead of two metatable-tagged union variants).Ok<T>asResult<T, never>andErr<E>asResult<never, E>, so the constructors return precise subtypes that upcast cleanly toResult<T, E>.ok/errconstructors to populate the unused field withnil :: never.unwrap_unchecked/unwrap_err_uncheckedto direct field reads — no more(self :: Ok<T>)._valuestyle downcasts.:: anyand:: Result.Result<T, E>workarounds fromtests/std/result/.spec.luaunow that subtype-based inference covers every call site.Why
The previous design used a real metatable-tagged union
Ok<T> | Err<E>. Luau's type checker does not narrowsetmetatable<...>unions on a boolean discriminator field, so calling generic methods like:is_ok_and(f: (T) -> boolean)on anErr<E>(or the symmetric Err-side methods onOk<T>) failed type checking with "no valid instantiation for T" —_valueresolved tonilfrom the Err branch's missing field and conflicted with the closure's parameter type. As a workaround the tests had to launder these call sites through:: any, which defeated the purpose of having strict types in the first place.Collapsing the two variants into one record gives both fields a real type at all times, with the unused side parameterized to
never. Upcasting anErr<E> = Result<never, E>toResult<T, E>then works through ordinarynever <: Tcovariance onreadfields — no narrowing required from the type checker. As a side effect this also lets theuncheckedmethods read fields directly without a (separately broken)Result<T, E>→Ok<T>downcast.Checklist
Required
lute run ./scripts/checker.luau -- ./src ./tests ./scriptspasseslute run ./scripts/tester.luaupassesFunctional Validation
tests/std/result/.spec.luauunwrap_unchecked/unwrap_err_uncheckedexercised on the correct variantConfiguration & Docs
mise.toml/pesde.tomlunchangedIf Applicable
Ok<T>andErr<E>are now aliases forResult<T, never>/Result<never, E>instead of independent setmetatable shapes. Downstream code that pattern-matched on the previous shape (e.g. discriminating on the absence of_value/_err) would need to switch to checking_ok. Pre-1.0, so impact is expected to be nil.