sol2
sol2 copied to clipboard
how to: safe property accessor chains
trafficstars
Is there a suggested way to make chains of properties "safe" in a reasonably low overheard way?
example: local a = thing.transform.position.x
where thing, transform and position is a user type, but each of them can be optional.
let's say transform does not exist on thing as determined on the C++ side.
You can use something like that:
local thing = {
transform = {
position = {
--x = 1
}
}
}
local foo = (((thing or {}).transform or {}).position or {}).x
print(foo)