Thread overview
std.typecons: PrimitiveRef
Mar 24, 2015
Andre
Mar 24, 2015
Andre
Mar 24, 2015
Gary Willoughby
Mar 24, 2015
Andre
Mar 24, 2015
Dicebot
March 24, 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é


March 24, 2015
Definition of BoolRef:

alias BoolRef = PrimitiveRef!bool;
March 24, 2015
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
March 24, 2015
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é
March 24, 2015
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) { }