The Reflect API is a way to manipulate library values dynamically through an
abstract interface in an untyped manner. Use with care!


Lib.Reflect.hasField(o, field)

Tells if structure o has a field named field.
This is only guaranteed to work for anonymous structures.

If o or field are nil, the result is unspecified.

o - object

field - field name

Lib.Reflect.field(o, field)

Returns the value of the field named field on object o.
If o is not an object or has no field named field, the result is nil.
If the field is defined as a property, its accessors are ignored. Refer to Reflect.getProperty for a function supporting property accessors.
If field is nil, the result is unspecified.

o - object

field - field name

Lib.Reflect.setField(o, field, value)

Sets the field named field of object o to value value.
If o has no field named field, this function is only guaranteed to work for anonymous structures.
If o or field are nil, the result is unspecified.

o - object

field - field name

value - field value

Lib.Reflect.getProperty(o, field)

Returns the value of the field named field on object o, taking property getter functions into account.
If the field is not a property, this function behaves like Reflect.field, but might be slower.
If o or field are nil, the result is unspecified.

o - object

field - property field name

Lib.Reflect.setProperty(o, field, value)

Sets the field named field of object o to value value, taking property setter functions into account.
If the field is not a property, this function behaves like Reflect.setField, but might be slower.
If field is nil, the result is unspecified.

o - object

field - property field name

value - field value

Lib.Reflect.callMethod(o, funcName, args)

Call a method funcName with the given object o and arguments args.

o - object

funcName - method name, string value

args - array of arguments

Lib.Reflect.callMethodSafe(o, funcName, args, errFunc)

Safe call for a Lib method with funcName with the given object o and arguments args. This call performs in safe try/catch block. errFunc will be triggered in case of Lib error.

WARNING: callMethodSafe works only with Lib methods.

REMOVED FROM LIB (use pcall)

o - object

funcName - method name, string value

args - array of arguments

errFunc - on error function

Lib.Reflect.fields(o)

Returns the fields of structure o.
This method is only guaranteed to work on anonymous structures.
If o is nil, the result is unspecified.

o - object

Lib.Reflect.isFunction(f)

Returns true if f is a function, false otherwise.
If f is nil, the result is false.

f - function or any other object

Lib.Reflect.isObject(v)

Tells if v is an object.
The result is true if v is one of the following:
- class instance
- structure
- Class type
- Enum type
Otherwise, including if v is nil, the result is false.

v - any type object, enum value

Lib.Reflect.isEnumValue(v)

Tells if v is an enum value.
The result is true if v is of type EnumValue, i.e. an enum constructor.
Otherwise, including if v is nil, the result is false.

v - any type object, enum value

Lib.Reflect.deleteField(o, field)

Removes the field named field from structure o.
This method is only guaranteed to work on anonymous structures.
If o or field are nil, the result is unspecified.

o - object

field - field name

Lib.Reflect.copy(o)

Copies the fields of structure o.
This is only guaranteed to work on anonymous structures.
If o is nil, the result is unspecified.

o - object


Examples:


Lib.Sys.trace(Lib.Reflect.fields(Lib))

Lib.Reflect.callMethod(Lib.Sys, Lib.Sys.trace, {'message'})


local obj = {a = 1, b= 'a'}

local clone = Lib.Reflect.copy(obj)

Lib.Sys.trace(clone)


Lib.Reflect.callMethodSafe(Lib.Sys, "throw", {"My Error"}, function(err) print("ERROR: "..err) end)


--WARNING: current implementation of callMethodSafe will not work on Lib methods assigned to non Lib Lua object:

a = {} -- non Lib Lua object

a.throw = Lib.Sys.throw -- throw assigned to Lua object

Lib.Reflect.callMethodSafe(a, "throw", {"err1"}, function(err) print("ERROR: "..err) end) -- will not capture error


PointToSys = Lib.Sys

Lib.Reflect.callMethodSafe(PointToSys, "throw", {"err2",2}, function(err) print("ERROR: "..err) end) -- will capture error, method "throw" still part of Lib object

Created with the Personal Edition of HelpNDoc: Easy EPub and documentation editor