Thread overview |
---|
November 17, 2017 How to define variadic delegate with a ref/out argument? | ||||
---|---|---|---|---|
| ||||
struct DelegateList(Args...) { public: alias DelegateHandler = void delegate(Args args) nothrow; DelegateHandler[] items; void opCall(Args args) nothrow { foreach (i; items) i(args); } } DelegateList!(string, int) list; // Compile OK so far DelegateList!(string, int*) list2; // Compile OK so far DelegateList!(string, ref int) list3; // Compile error -> How to make it work? Thanks Pham |
November 17, 2017 Re: How to define variadic delegate with a ref/out argument? | ||||
---|---|---|---|---|
| ||||
Posted in reply to pham | On Friday, 17 November 2017 at 05:08:23 UTC, pham wrote: > struct DelegateList(Args...) > { > public: > alias DelegateHandler = void delegate(Args args) nothrow; > > DelegateHandler[] items; > > void opCall(Args args) nothrow > { > foreach (i; items) > i(args); > } > } > > DelegateList!(string, int) list; // Compile OK so far > DelegateList!(string, int*) list2; // Compile OK so far > DelegateList!(string, ref int) list3; // Compile error -> How to make it work? > > Thanks > Pham The only way I know of is using a template which behaves like a reference. Which can be done with nullableRef I suppose. Haven't actually tried it. import std.typecons : NullableRef, nullableRef; DelegateList!(NullableRef!int)(nullableRef(some_int)); |
November 19, 2017 Re: How to define variadic delegate with a ref/out argument? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jerry A. | On Friday, 17 November 2017 at 06:21:50 UTC, Jerry A. wrote:
> On Friday, 17 November 2017 at 05:08:23 UTC, pham wrote:
>> struct DelegateList(Args...)
>> {
>> public:
>> alias DelegateHandler = void delegate(Args args) nothrow;
>>
>> DelegateHandler[] items;
>>
>> void opCall(Args args) nothrow
>> {
>> foreach (i; items)
>> i(args);
>> }
>> }
>>
>> DelegateList!(string, int) list; // Compile OK so far
>> DelegateList!(string, int*) list2; // Compile OK so far
>> DelegateList!(string, ref int) list3; // Compile error -> How to make it work?
>>
>> Thanks
>> Pham
>
> The only way I know of is using a template which behaves like a reference.
> Which can be done with nullableRef I suppose. Haven't actually tried it.
>
> import std.typecons : NullableRef, nullableRef;
>
> DelegateList!(NullableRef!int)(nullableRef(some_int));
nullableRef is same as passing pointer. Using "ref" is stronger guarantee that the var is never be passed as null.
variadic parameter passed by "ref" should be supported
Thanks.
Pham
|
Copyright © 1999-2021 by the D Language Foundation