Parse, Don’t Validate
Haxe has a nice mechanism to handle this called "abstract types".  <a href="https://haxe.org/manual/types-abstract.html" rel="nofollow">https://haxe.org/manual/types-abstract.html</a><p>The critical validation step happens when the abstract type is created, not when it is used or passed to other functions...similar to the example in TFA.<p>The added benefit is that you get a type that behaves pretty closely to a class, but adds no typing or runtime memory overhead.  E.g. here's an example for an "unsafe" string : <a href="https://gist.github.com/jdonaldson/9a09287e540e7392857f" rel="nofollow">https://gist.github.com/jdonaldson/9a09287e540e7392857f</a><p>Another benefit is that you abstract types can define relationships between multiple types in this way, making it possible to perform validation for functions that need to handle "Either<T>"-style types.