Add a type type
Denneisk opened this issue · 4 comments
Description: A new type xt
that contains an E2 type. For example,
TypeVar = number # or typeof(1)
NumVar = 100
print(TypeVar, typeof(NumVar), TypeVar == typeof(NumVar)) # prints "number, number, 1"
This would be more than just a string, and could be used in places where types are currently statically defined. Both of these functions now replace what would have taken a switch statement over every considerable type.
function printTableElement(Index:string) {
Types = Table:types() # or Table:typeids() that would require manual conversion from strings to types
print(Table[Index, Types[Index, type]]) # Alternatively one could consider a Table:typeof(Index) function
}
function varArgsFunction(...Args:table) {
Types = Args:typesArray()
foreach(I:number, V:type = Types) {
print(Args[I, V])
}
}
For the most part this would be rather underwhelming, but it shines in interaction with tables. This would greatly simplify users ability of accessing and creating array/table elements of a type defined at runtime. The purpose of having it as a completely separate type is so that it avoids the following situation
String = "Some random string"
Table[1, String] # Type error will mask the user's syntax error
No, this just doesn't make any sense..
The only language where this kind of exists is Zig, but that works because types can only exist as separate compile-time variables
If you want dynamic typing we could always just go through with the initial idea of making Table[1] valid but returning an unknown
type (which you'd cast to whatever with as <type>
)
Additionally this just wouldn't work, maybe it would for print
since it takes a vararg, which means it can accept any type, but you wouldn't know what type you're getting from it at compile time.. which brings you back to just making X[1]
work and return unknown
💀
I'm going to close this. You or someone else can feel free to make another issue to add an unknown
type, and a type casting system.