local Point = @record {x: integer, y: integer, z: integer}
It is currently possible to initialize it in order
local x = 1
local y = 2
local z = 3
local p = Point {x, y, z}
but having it rely on order can lead to bugs
-- this is a bug: `x` and `y` are flipped in order
-- however, since they have the same type, the type checker can't catch it
local p = Point {y, x, z}
the following pattern is very common in Lua
local p = Point{
x = x,
y = y,
z = z
}
However, it is repetitive, error-prone and verbose.
Proposal
Have a shorter initialization syntax. For example:
local p = Point { z, y, x }
That would look exactly like the array initialization syntax, though, so it could be changed to something else
local p = Point { =z, =y, =x }
Either syntax would be expanded to the same:
local p = Point { z = z, y = y, x = x }
Ordering doesn't matter on either case. Each variable gets assigned to the correct struct field with the same name.
Problem
Given a struct Point
It is currently possible to initialize it in order
but having it rely on order can lead to bugs
the following pattern is very common in Lua
However, it is repetitive, error-prone and verbose.
Proposal
Have a shorter initialization syntax. For example:
That would look exactly like the array initialization syntax, though, so it could be changed to something else
Either syntax would be expanded to the same:
Ordering doesn't matter on either case. Each variable gets assigned to the correct struct field with the same name.
Inspiration