SafeUnion

Union of ConTypes... Ensures correct access with assert

Constructors

this
this(T obj)

Constuctor supporting direcs assigment of Type

Members

Aliases

FromTypes
alias FromTypes = ConTypes
Undocumented in source.
Types
alias Types = TypesM
Undocumented in source.

Functions

apply
auto ref apply()
Undocumented in source. Be warned that the author may not have intended to support it.
call
auto call(Args args)

Forwards call to union member Works only if all union members has this function and this function has the same return type and parameter types Can not be made opDispatch because it somehow breakes hasMember trait

customSerialize
void customSerialize(Serializer serializer, COS con)

Support for serialization

get
auto get()

returns given type with check

isType
bool isType()

Returns enum value for Type

opAssign
void opAssign(SafeUnion!(makeFirstParDefaultOne, ConTypes) obj)
Undocumented in source. Be warned that the author may not have intended to support it.
opAssign
void opAssign(T obj)
Undocumented in source. Be warned that the author may not have intended to support it.
set
auto set(T obj)

Sets given Type

toString
void toString(void delegate(const(char)[]) sink, FormatSpec!char fmt)

Preety print

Static functions

checkOpDispach
bool checkOpDispach()

Checks if opDispatch supports given function

getEnum
Types getEnum()

Returns enum value for Type

Unions

__anonymous
union __anonymous
Undocumented in source.

Variables

currentType
Types currentType;
Undocumented in source.

Examples

Example Usage

t {
	struct Triangle {
		int add(int a) {
			return a + 10;
		}
	}

	struct Rectangle {
		int add(int a) {
			return a + 100;
		}
	}

	static uint strangeID(T)(T obj) {
		static if (is(T == Triangle)) {
			return 123;
		} else static if (is(T == Rectangle)) {
			return 14342;
		} else {
			assert(0);
		}
	}

	alias Shape = SafeUnion!(false, Triangle, Rectangle);
	Shape shp;
	shp.set(Triangle());
	assert(shp.isType!Triangle);
	assert(!shp.isType!Rectangle);
	assert(shp.call!("add")(6) == 16); //Better error messages 
	assert(shp.apply!strangeID == 123);
	//shp.get!(Rectangle);//Crash
	shp.set(Rectangle());
	assert(shp.call!("add")(6) == 106);
	assert(shp.apply!strangeID == 14342);
	shp.currentType = shp.Types.none;
	//shp.apply!strangeID;//Crash
	//shp.add(6);//Crash
	final switch (shp.currentType) {
	case shp.getEnum!Triangle:
		break;
	case Shape.getEnum!Rectangle:
		break;
	case Shape.Types.none:
		break;
	

Meta