ThePhD / sol2

Sol3 (sol2 v3.0) - a C++ <-> Lua API wrapper with advanced features and top notch performance - is here, and it's great! Documentation:
http://sol2.rtfd.io/
MIT License
4.18k stars 515 forks source link

how to: safe property accessor chains #1527

Open fhoenig opened 1 year ago

fhoenig commented 1 year ago

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.

skaarj1989 commented 1 year ago

You can use something like that:

local thing = {
  transform = {
    position = {
      --x = 1
    }
  }
}

local foo = (((thing or {}).transform or {}).position or {}).x
print(foo)