← Lua Academy

Typed Lua · 39 / 69

Reuse a domain type

YOUR TASK

Make this happen

3 steps
  1. A type alias gives a meaningful name to a record. Use the same dimensions contract for each argument and compute whether their sizes match.

  2. Read the cases in lesson.test.lua, then choose Check solution to see what needs to change.

  3. Update subject.lua until every test passes. Keep the supplied tests unchanged.

Read the starter and tests

subject.lua

type Dimensions = { width: number, height: number }
exports.same = function(a: Dimensions, b: Dimensions): boolean
  return a.width == b.width
end

lesson.test.lua

local t = require("test")
local subject = require("cococo.subject")
t.it("both dimensions matter", function()
  local s = subject.load()
  t.expect(s.same({ width = 2, height = 3 }, { width = 2, height = 4 }))
    :eq(false)
end)
t.it("identical dimensions", function()
  local s = subject.load()
  t.expect(s.same({ width = 2, height = 3 }, { width = 2, height = 3 }))
    :eq(true)
end)