← Back to context

Comment by jdonaldson

6 years ago

Haxe has a nice mechanism to handle this called "abstract types". https://haxe.org/manual/types-abstract.html

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.

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 : https://gist.github.com/jdonaldson/9a09287e540e7392857f

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.