From ffff25a9e502df4555c3bb30feb5f3625fd7d135 Mon Sep 17 00:00:00 2001 From: Alexander McCord <11488393+alexmccord@users.noreply.github.com> Date: Thu, 7 Apr 2022 09:16:44 -0700 Subject: [PATCH] Improve the UX of reading tagged unions a smidge. (#449) The page is a little narrow, and having to scroll on this horizontally isn't too nice. This fixes the UX for this specific part. --- docs/_pages/typecheck.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/docs/_pages/typecheck.md b/docs/_pages/typecheck.md index 20f73987..63e4c8bb 100644 --- a/docs/_pages/typecheck.md +++ b/docs/_pages/typecheck.md @@ -392,18 +392,20 @@ local account: Account = Account.new("Alexander", 500) Tagged unions are just union types! In particular, they're union types of tables where they have at least _some_ common properties but the structure of the tables are different enough. Here's one example: ```lua -type Result = { type: "ok", value: T } | { type: "err", error: E } +type Ok = { type: "ok", value: T } +type Err = { type: "err", error: E } +type Result = Ok | Err ``` This `Result` type can be discriminated by using type refinements on the property `type`, like so: ```lua if result.type == "ok" then - -- result is known to be { type: "ok", value: T } + -- result is known to be Ok -- and attempting to index for error here will fail print(result.value) elseif result.type == "err" then - -- result is known to be { type: "err", error: E } + -- result is known to be Err -- and attempting to index for value here will fail print(result.error) end