55 lines
1.3 KiB
Lua
55 lines
1.3 KiB
Lua
local oop = require("oop/oop")
|
|
local Types = require("oop/types")
|
|
|
|
local function attempt(label, fn)
|
|
local ok, err = pcall(fn)
|
|
if ok then
|
|
print(label .. ": ok")
|
|
else
|
|
print(label .. ": " .. err)
|
|
end
|
|
end
|
|
|
|
local i = Types.Int(10)
|
|
local f = Types.Float(2.5)
|
|
local s = Types.Str("hi")
|
|
local b = Types.Bool(true)
|
|
local t = Types.Tuple({ 1, 2, 3 })
|
|
local l = Types.List({ "a", "b" })
|
|
local q = Types.Queue({ 1, 2 })
|
|
local st = Types.Stack({ 9, 8 })
|
|
local d = Types.Dict({ one = 1, two = 2 })
|
|
local se = Types.Set({ "x", "y" })
|
|
|
|
print("Int:", i)
|
|
print("Float:", f)
|
|
print("Str:", s, "#", #s)
|
|
print("Bool:", b)
|
|
print("Tuple:", t, "#", #t, "get2", t.get(2))
|
|
print("List:", l, "#", #l, "get1", l.get(1))
|
|
print("Queue:", q, "#", #q, "peek", q.peek())
|
|
print("Stack:", st, "#", #st, "peek", st.peek())
|
|
print("Dict:", d, "#", #d, "get(two)", d.get("two"))
|
|
print("Set:", se, "#", #se, "has(x)", se.has("x"))
|
|
|
|
print("Int add:", i + Types.Int(5))
|
|
print("Float mul:", f * Types.Float(2.0))
|
|
print("Str concat:", s .. Types.Str("!"))
|
|
print("Bool eq:", b == Types.Bool(true))
|
|
|
|
attempt("Int mutate", function()
|
|
i.value = 99
|
|
end)
|
|
attempt("Float mutate", function()
|
|
f.value = 1.25
|
|
end)
|
|
attempt("Str mutate", function()
|
|
s.value = "nope"
|
|
end)
|
|
attempt("Bool mutate", function()
|
|
b.value = false
|
|
end)
|
|
attempt("Tuple mutate", function()
|
|
t.items[1] = 99
|
|
end)
|