Let's say I have a type system that has has three primitive types in addition to the null value:
- null
boolnumstring
Additionally, there is a typed array for each of the types, so we now have: null, bool, bool[], num, num[], string, and string[] as all of our types.
Finally, we have an Any type that can contain any other type. For example, we might have two columns like this:
| id (num) | value (any) |
|---|---|
| 1 | "abc" |
| 2 | [1,2,3] |
| 3 | [1, true] ?? |
My question then is whether the any[] value, such as [1, true] is also contained within the any type (since it can be "anything"), or is [1, true] not a valid value for that type? In other words, what "types" do I need in my type-system -- option 1 or option 2 below?
- null,
bool,bool[],num,num[],string,string[], andany - null,
bool,bool[],num,num[],string,string[],any, andany[]
And, if we only need the first version, does that then support recursion, such as [[1, true], [2, "hi"]] (that's two levels, i.e., any[][] and so so on), what about a jagged array, such as [[1,"a"], 0] ?
A type system with (perhaps unnatural) limitations is still a type system.
Any of the possibilities you have laid out would be possible to define grammars for, fully specify, and implement. You could even do other things like allow recursion only to a fixed depth, or have separate jagged and non-jagged nested array types.
The answer to the question of whether a type system could work any of these ways, is yes in each case.
If you are deciding on the type system yourself, the question then becomes whether you should allow
any[]and whetheranyshould allow nested arrays or not. The answer to that question depends on what your reasons for working with a type system are. There’s no one best answer for all contexts here.