So I’ve been thinking about the syntax for a while, and I’d like to suggest some updates to it. (keep in mind this is a mixture of multiple different ideas of varying quality, mainly just throwing stuff at the wall to see what sticks)
Type aliases
Current:
type SimpleRotation = {pitch: number, yaw: number}
Proposed:
type SimpleRotation: {pitch: number, yaw: number}
The logic behind this change is that all other areas where types appear use the colon. For consistency, it might be a good idea to add it here too. (the argument could however be made that since this defines a type instead of annotating a type, equals would be more suitable?)
Function types
Current:
type NumberPredicate = (number) => boolean
Proposed:
type NumberPredicate: function(number) => boolean
The logic behind this change is that, in the current system, it’s not completely obvious that function types represent functions. By adding the function keyword, it’d make it a lot more clear that it is, indeed, representing a function.
Union types
Current:
local metadata: string | number = "I am metadata!"
Proposed:
local metadata: string or number = "I am metadata!"
The logic behind this change is that, by using the existing ‘or’ keyword in place of the vertical bar, it makes it more obvious that the type can be either the type on the left, or the type on the right. It would additionally fit better with Lua’s more keyword-oriented syntax.
Any types
Current:
local anyValue: any = nil
Proposed:
local anyValue: any? = nil
local definedValue: any = 2
The logic behind this is that you might want to define a value with any type except nil. It could be productive to make any non-optional by default, and instead define untyped things as any?
. Maybe a different keyword would work better for non-optional any here?
Optional types
Current:
local function sample(x: number, y: number?, z: number?)
...
end
Proposed:
local function sample(x: number, y: number or nil, z: number or nil)
...
end
The logic behind this is that, following the design philosophy of Lua, it might be more appropriate to forgo a dedicated optional syntax and instead reuse union types for this purpose. It’s also way more obvious that a type is optional in this case; question marks are only single characters and can sometimes get lost among the rest of the characters (at least in my experience), which makes the code harder to understand quickly.
Just some random ideas. Feel free to pick any you like and criticise any you don’t