www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - std.typecons: PrimitiveRef

reply "Andre" <andre s-e-a-p.de> writes:
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é
Mar 24 2015
next sibling parent "Andre" <andre s-e-a-p.de> writes:
Definition of BoolRef:

alias BoolRef = PrimitiveRef!bool;
Mar 24 2015
prev sibling next sibling parent reply "Gary Willoughby" <dev nomad.so> writes:
On Tuesday, 24 March 2015 at 15:38:04 UTC, Andre wrote:
 The use case is a type tuple where you cannot use the keyword 
 ref.
Could template alias parameters not be used here? http://dlang.org/template.html#aliasparameters
Mar 24 2015
parent "Andre" <andre s-e-a-p.de> writes:
On Tuesday, 24 March 2015 at 16:58:48 UTC, Gary Willoughby wrote:
 On Tuesday, 24 March 2015 at 15:38:04 UTC, Andre wrote:
 The use case is a type tuple where you cannot use the keyword 
 ref.
Could template alias parameters not be used here? http://dlang.org/template.html#aliasparameters
Event is defined as struct Event(T...){}. That way the actual number of parameters is very flexible. I just checked template alias parameters. Also here the ref keyword is not allowed: struct Event(alias x) { private void delegate(x)[] _dlgArr; } Event!(ref bool) onEvent; Error is thrown: expression expected not ref. Kind regards André
Mar 24 2015
prev sibling parent "Dicebot" <public dicebot.lv> writes:
On Tuesday, 24 March 2015 at 15:38:04 UTC, Andre wrote:
 I think it is useful to add to std.typecons as there is no 
 possibility
 to use ref for type tuples.
This sounds like a bad idea because in D `ref` is not a type qualifier and thus not part of a type. Not though that you can apply ref storage class to type tuple as a whole: void foo(T...)(ref T args) { }
Mar 24 2015