Skip to content

Simn-haxe2/hxunion

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Haxe Type Union

OBSOLETE: This library is no longer maintained

This library allows usage of type unions by creating a common enumeration.

Usage

Simply make classes use implements hxunion.UnionSupport and you are good to go. You can list allowed types as your method's return type by using the syntax hxunion.Union<[Type1, Type2, ..., TypeN]>. This will create a hidden enum with constructors for Type1 to TypeN that you can switch over.

Assuming you defined two enums:


enum MyProblem
{
	CoffeeShortage(amount:Int);
	LessImportant;
}

enum MySuccess
{
	BossDead;
	FoundGold;
}

You can declare a function returning either of these like so:


public function doSomething():Union<[MyProblem, MySuccess]>
{
	var rand = Std.random(4);
	return switch(rand)
	{
		case 1: CoffeeShortage(3);
		case 2: BossDead;
		case 3: FoundGold;
		default: LessImportant;
	}
}

And you can simply switch over the result of that function:

static public function main()
{
	switch(doSomething())
	{
		case MySuccess(s):
			switch(s)
			{
				case BossDead:
				case FoundGold:
			}
		case MyProblem(p):
			switch(p)
			{
				case CoffeeShortage(amount):
				case LessImportant:
			}
	}
}

Of course the types must not be enums, you can return different class types as well:


public function multiClassReturn():hxunion.Union<[Int, String, Xml]>
{
	if (Date.now().getHours() == 23 && Date.now().getMinutes() == 58)
		return Xml.parse("<two minutes='till'>midnight</two>");
	if (Date.now().getMinutes() < 20)
		return 12;
	else
		return "Just a string";
}

About

Allow functions to return multiple return types. Type-safe, of course.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages