std.typecons: PrimitiveRef

Andre via Digitalmars-d digitalmars-d at puremagic.com
Tue Mar 24 08:38:02 PDT 2015


Hi,

Namespace helped me to get following template working.

struct PrimitiveRef(T)
{
	private T* _value;

	@property
	ref inout(T) get() inout pure nothrow {
		assert(_value);
		return *_value;
	}
	
	alias get this;
	
	this(T val) {
		_value = new T(val);
	}
}

The use case is a type tuple where you cannot use the keyword ref.

Example usage: ( This is an extract from a little event framework)

Event!(Object, BoolRef) onClose;

onClose.attach(&onFormClose); // function or delegate

void onFormClose(Object o, BoolRef canClose)
{
     canClose = false;
}

I think it is useful to add to std.typecons as there is no 
possibility
to use ref for type tuples.

Kind regards
André




More information about the Digitalmars-d mailing list