I'm trying to create a new object with a variable discriminator and a specific field that corresponds to this discriminator.
And although I can see why the compiler cannot "prove" that it's "safe to initialize", I can assure you it is.
The question is, how would I get around this error?
This is the code that triggers the error:
target.addChild(
Node(
kind: callType,
value: v,
arity: arity
)
)
And the error refers to the .arity field.
Sure thing is, the callType (the discriminator) which is calculate before this piece of code is always one that allows for this .arity field.
So... is there some workaround?
OK, so I found a way to make it work...
Instead of declaring callType as DiscriminatorKind like:
var callType: DiscriminatorKind
I declare it with a range of possible/allowed DiscriminatorKind values:
var callType: lowerAllowedDiscriminator..highestAllowedDiscriminator
and it works beautifully!