Created
September 15, 2024 01:25
-
-
Save Accoast/e819afb293df941210b82b441809fcf4 to your computer and use it in GitHub Desktop.
runtime safe object/instance properties set specified to use with --!strict
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
| --!optimize 2 | |
| export type props = {[string]: any} | |
| -- return runtime safe pcall wrapper | |
| local function generate_safewrap(func: (any...) -> (any...)) | |
| return function(...: any...) | |
| local success: boolean, result: string? = pcall(func, ...) | |
| if success then | |
| return result | |
| else | |
| warn("[safepropseterr]:", result) | |
| end | |
| end | |
| end | |
| -- wrappers generated | |
| return { | |
| access_set = generate_safewrap(function(object: Instance, key: string, value: any) | |
| object[key] = value | |
| end), | |
| pure_access = generate_safewrap(function(object: Instance, key: string) | |
| return object[key] | |
| end), | |
| prop_set = generate_safewrap(function(object: Instance, props: props) | |
| for k,v in props do | |
| object[k] = v | |
| end | |
| end), | |
| } :: { | |
| access_set: (object: Instance, key: string, value: any) -> (), | |
| pure_access: (object: Instance, key: string) -> (any), | |
| prop_set: (object: Instance, props: props) -> (), | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment