HaxeFoundation / haxe

Haxe - The Cross-Platform Toolkit
https://haxe.org
6.03k stars 648 forks source link

How to create ComplexType representing anonymous structure with optional fields? #11603

Closed NQNStudios closed 3 months ago

NQNStudios commented 3 months ago

In a build macro, I want to create a ComplexType TAnonymous([fields...]) in which some of the fields are optional. But haxe.macro.Field doesn't have an opt parameter, and when I put TOptional(type) as a field object's type, the compiler says " Optional type not allowed here"

I also tried prefixing the name of a Field object with "?", to emulate the syntax

{?field:Type} which works in normal haxe type definitions.

When I did this, referencing my anonymous type's fields would cause a compiler error "{?field: Type } has no field field (Suggestion: ?field)" which is weird.

NQNStudios commented 3 months ago

I figured out that adding :optional to a Field's meta achieves the desired result.