Created
October 28, 2024 16:26
-
-
Save JabDoesThings/9e1920cd1cc6b77cbabcdcce1aba1219 to your computer and use it in GitHub Desktop.
Freezes objects as read-only.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- Creates a read-only object, much like frozen objects in JavaScript. | |
| -- | |
| -- Example: | |
| -- | |
| -- local readonly = require 'readonly'; | |
| -- // Cannot modify. | |
| -- local object = readonly({ pi = math.PI }); | |
| -- | |
| -- | |
| -- @author asledgehammer, JabDoesThings 2024 | |
| local meta; | |
| return function(table) | |
| meta = getmetatable(table) or {}; | |
| return setmetatable({}, { | |
| __index = table, | |
| __newindex = function() error("Attempt to modify read-only class.", 2) end, | |
| __metatable = false, | |
| __add = meta.__add, | |
| __sub = meta.__sub, | |
| __mul = meta.__mul, | |
| __div = meta.__div, | |
| __mod = meta.__mod, | |
| __pow = meta.__pow, | |
| __eq = meta.__eq, | |
| __lt = meta.__lt, | |
| __le = meta.__le, | |
| __concat = meta.__concat, | |
| __call = meta.__call, | |
| __tostring = meta.__tostring | |
| }); | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment