From 5782ba9463988290edbd9b6d52b588527b9bfb4b Mon Sep 17 00:00:00 2001 From: Junseo Yoo Date: Fri, 31 May 2024 09:55:07 -0700 Subject: [PATCH] Revised the alternatives section --- docs/index-type-operator.md | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/docs/index-type-operator.md b/docs/index-type-operator.md index 3bcde82..d10a7de 100644 --- a/docs/index-type-operator.md +++ b/docs/index-type-operator.md @@ -74,4 +74,21 @@ A drawback to this feature is the possible increase in cost of maintenance. In t ## Alternatives -An alternative to the proposed implementation is utilizing inexact table types and exact table types (to be implemented) to handle the edge in a simpler manner. If we know that the indexee is an inexact table type, the index type operator can return the `unknown` type. If we know that the indexee is an exact table type, we can return an error. \ No newline at end of file +An alternative design can be depicted from the example below: +```lua +type Person2 = { + age: number, + name: string, + alive: boolean, + job: string +} + +local function edgeCase(p: Person) + type unknownType = index +end +``` +In our current design, the program simply fails to reduce (and throws an error). However, it is worth noting that `index` can also reduce to type `unknown` if `p` is of type `Person2` (this is allowed since tables support width subtyping; hence `Person2` is a subtype of `Person`). In this design, we would need a way to determine if the indexee can be different at runtime. We could determine this through an implementation of more table types, specifically exact and inexact table types. Then, the program will have two cases when a indexee does not contain the indexer type: +1. If the indexee is an inexact table type, the expression is reduced to an `unknown` type. +2. If the indexee is an exact table type, the expression fails to reduce and throws and error. + +Note: exact table types "indicates that the table has only the properties listed in its type" and inexact table type "indicates that the table has at least the properties listed in its type". \ No newline at end of file