No operators and no constructors for math types #5

Open
opened 2026-10-22 20:52:21 +01:00 by pedro-bento · 0 comments
Owner

Summary

Structs are raw FFI values, so vector math is written as nested calls, comparison is manual, and
building a Color needs ffi.new with a table.

Current behavior

local v = M.Vector2Scale(M.Vector2Add(a, b), 2)
local red = ffi.new("Color", { r = 255, g = 0, b = 0, a = 255 })

Proposal

1. Operators

Install one metatype per value type at the end of the generated file:

Type Operators
Vector2, Vector3, Vector4 + - * / -x == tostring
Quaternion + - * == tostring
Matrix + - * tostring (raymath has no matrix equality)
Color == tostring (no arithmetic; raylib colour math is explicit)

Details that matter:

  • * accepts a number on either side: Vector2Scale for scalars, Vector2Multiply for vectors.
  • == uses Vector2Equals(a, b) ~= 0, because those functions return int, not bool.
  • No __len: #v must return an integer, but length returns a float. Keep M.Vector2Length(v).
  • No __index: adding methods would make field typos return nil instead of erroring.
  • ffi.metatype is permanent and errors on a second install, so wrap it:
    local ok, err = pcall(ffi.metatype, ct, mt)
    if not ok and not tostring(err):match("metatable") then error(err, 0) end
    

2. Constructors

M.Vector2(x, y)   M.Vector3(x, y, z)   M.Vector4(x, y, z, w)
M.Quaternion(x, y, z, w)   M.Matrix(...)   M.Rectangle(x, y, w, h)

M.Color(255, 0, 0)      -- alpha 255
M.Color(255, 0, 0, 128)
M.Color("#f00")         -- also "#ff0000", "#ff000080", with or without "#"
M.Color{ r = 255, g = 0, b = 0 }

Bad hex strings raise a Lua error, not an FFI abort.

Acceptance criteria

  • For random vectors, (a + b) equals M.Vector2Add(a, b); same for *, /, ==.
  • tostring output is stable and readable.
  • All three M.Color forms work; bad input raises a Lua error.
  • Loading the module twice does not error (metatype guard).
  • make check-bindings passes.

Notes

  • Constructors allocate one cdata per call. Define constants once at module scope.
  • Resource unloading is not part of this issue.
  • ffi.new("Vector2", v) still makes an unowned copy; operators never share storage.
## Summary Structs are raw FFI values, so vector math is written as nested calls, comparison is manual, and building a `Color` needs `ffi.new` with a table. ## Current behavior ```lua local v = M.Vector2Scale(M.Vector2Add(a, b), 2) local red = ffi.new("Color", { r = 255, g = 0, b = 0, a = 255 }) ``` ## Proposal ### 1. Operators Install one metatype per value type at the end of the generated file: | Type | Operators | |---|---| | `Vector2`, `Vector3`, `Vector4` | `+ - * / -x == tostring` | | `Quaternion` | `+ - * == tostring` | | `Matrix` | `+ - * tostring` (raymath has no matrix equality) | | `Color` | `== tostring` (no arithmetic; raylib colour math is explicit) | Details that matter: - `*` accepts a number on either side: `Vector2Scale` for scalars, `Vector2Multiply` for vectors. - `==` uses `Vector2Equals(a, b) ~= 0`, because those functions return `int`, not `bool`. - No `__len`: `#v` must return an integer, but length returns a float. Keep `M.Vector2Length(v)`. - No `__index`: adding methods would make field typos return `nil` instead of erroring. - `ffi.metatype` is permanent and errors on a second install, so wrap it: ```lua local ok, err = pcall(ffi.metatype, ct, mt) if not ok and not tostring(err):match("metatable") then error(err, 0) end ``` ### 2. Constructors ```lua M.Vector2(x, y) M.Vector3(x, y, z) M.Vector4(x, y, z, w) M.Quaternion(x, y, z, w) M.Matrix(...) M.Rectangle(x, y, w, h) M.Color(255, 0, 0) -- alpha 255 M.Color(255, 0, 0, 128) M.Color("#f00") -- also "#ff0000", "#ff000080", with or without "#" M.Color{ r = 255, g = 0, b = 0 } ``` Bad hex strings raise a Lua error, not an FFI abort. ## Acceptance criteria - [ ] For random vectors, `(a + b)` equals `M.Vector2Add(a, b)`; same for `*`, `/`, `==`. - [ ] `tostring` output is stable and readable. - [ ] All three `M.Color` forms work; bad input raises a Lua error. - [ ] Loading the module twice does not error (metatype guard). - [ ] `make check-bindings` passes. ## Notes - Constructors allocate one cdata per call. Define constants once at module scope. - Resource unloading is not part of this issue. - `ffi.new("Vector2", v)` still makes an unowned copy; operators never share storage.
pedro-bento changed title from ergonomic metatypes and operator overloading to No operators and no constructors for math types 2026-10-23 12:38:16 +01:00
Sign in to join this conversation.
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: pedro-bento/hate-no-d#5
No description provided.