I'm writing a validation library for an API and I need to check anonymous data structures for required fields. Is it safe to directly reference fields (if(anon.field)...) or will this cause problems on static targets? Is there a speed difference between Reflect.hasField and direct field access?
Both checks have different purposes. You can do if (anon.field == null) if the field is already defined. So if the field isn't on the structure or it is uncertain (for example if you use @:optional fields in typedefs), you'll maybe get runtime errors.
Thats where Reflect.hasField comes in; it will check if it actually is defined. So if a structure has a field, you can check anything on it.
If you are targetting Javascript the Reflect.hasField does the same as Object.hasOwnProperty.
Reflect Api docs: http://api.haxe.org/Reflect.html#hasField
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With