Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reflect.hasField vs anon.field

Tags:

haxe

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?

like image 428
Indolering Avatar asked Mar 05 '26 00:03

Indolering


1 Answers

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

like image 102
Mark Knol Avatar answered Mar 07 '26 22:03

Mark Knol



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!