Hello. I’ve been trying to understand knit framework’s source code. and I’ve found this module called “Symbol” and I wonder what it’s use case?
Here is the symbol module
-- Symbol
-- Stephen Leitnick
-- December 27, 2020
--[[
symbol = Symbol.new(id: string [, scope: Symbol])
Symbol.Is(obj: any): boolean
Symbol.IsInScope(obj: any, scope: Symbol): boolean
--]]
local CLASSNAME = "Symbol"
local Symbol = {}
Symbol.__index = Symbol
function Symbol.new(id, scope)
assert(id ~= nil, "Symbol ID cannot be nil")
if (scope ~= nil) then
assert(Symbol.Is(scope), "Scope must be a Symbol or nil")
end
local self = setmetatable({
ClassName = CLASSNAME;
_id = id;
_scope = scope;
}, Symbol)
return self
end
function Symbol.Is(obj)
return (type(obj) == "table" and getmetatable(obj) == Symbol)
end
function Symbol.IsInScope(obj, scope)
return (Symbol.Is(obj) and obj._scope == scope)
end
function Symbol:__tostring()
return ("Symbol<%s>"):format(self._id)
end
return Symbol
and here is a module that I found using it
-- EnumList
-- Stephen Leitnick
-- January 08, 2021
--[[
enumList = EnumList.new(name: string, enums: string[])
enumList:Is(item)
Example:
direction = EnumList.new("Direction", {"Up", "Down", "Left", "Right"})
leftDir = direction.Left
print("IsDirection", direction:Is(leftDir))
--]]
local Symbol = require(script.Parent.Symbol)
local EnumList = {}
function EnumList.new(name, enums)
local scope = Symbol.new(name)
local enumItems = {}
for _,enumName in ipairs(enums) do
enumItems[enumName] = Symbol.new(enumName, scope)
end
local self = setmetatable({
_scope = scope;
}, {
__index = function(_t, k)
if (enumItems[k]) then
return enumItems[k]
elseif (EnumList[k]) then
return EnumList[k]
else
error("Unknown " .. name .. ": " .. tostring(k), 2)
end
end;
__newindex = function()
error("Cannot add new " .. name, 2)
end;
})
return self
end
function EnumList:Is(obj)
return Symbol.IsInScope(obj, self._scope)
end
return EnumList