uniontypes

Union types in Haxe, similar to Typescript.

To install, run:

haxelib install uniontypes 0.3.0 

See using Haxelib in Haxelib documentation for more information.

README.md

Uniontypes

For those libraries that return one thing or another. Similar to Union types in Typescript.

Installation

haxelib install uniontypes

Usage

final x : Union<Int, String> = "A string";

switch x.type() {
    case Int(i): trace('It was an Int: $i');
    case String(s): trace('It was a String: ${s.toUpperCase()}');
    case Null: trace('It was null');
}

The Union type is for two types, but the library also includes Union3, Union4, Union5 and Union6 for more extreme cases.

To trust, or not to trust

Depending on how much you trust the data in the union, you can specify different types of unions:

  • Union.TrustedUnion assumes a value will never be null, meaning a Null enum constructor will not exist.
  • Union.UntrustedUnion includes both a Null and an Unknown(u : Dynamic) enum constructor.
Contributors
ciscoheat
Version
0.3.0
Published
5 years ago
License
MIT

All libraries are free

Every month, more than a thousand developers use Haxelib to find, share, and reuse code — and assemble it in powerful new ways. Enjoy Haxe; It is great!

Explore Haxe

Haxe Manual

Haxe Code Cookbook

Haxe API documentation

You can try Haxe in the browser! try.haxe.org

Join us on GitHub!

Haxe is being developed on GitHub. Feel free to contribute or report issues to our projects.

Haxe on GitHub