templatized delegate

Alex via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon May 22 16:39:39 PDT 2017


On Monday, 22 May 2017 at 21:44:17 UTC, ag0aep6g wrote:
> On 05/22/2017 11:04 AM, Alex wrote:
>> [...]
>
> Not only is a template not an lvalue, it's not any kind of 
> value at all. It doesn't have a type. You can't have a variable 
> holding a template. You can't pass it as an argument.
>
> But a template is a symbol. You can pass it in an alias 
> parameter. So to pass dlg to C, you have to make C a template 
> with an alias parameter, like A is.

Aha... ok, I see...

>
>> [...]
> `A!dlg a;` works. Calling `fun` doesn't.
>
> A.fun instantiates dlg, resulting in a delegate that should be 
> able to access main's stuff. But it's not guaranteed that main 
> is active when A.fun is called. You could have returned `a` 
> from main before calling fun. For an actual delegate, a closure 
> would be made in that case. But dlg isn't a delegate, it's a 
> template. I guess it's not possible (or not feasible, or not 
> implemented) to create a closure a template like this.

ok, I see the point, I think...

>
> If you don't actually need dlg to access main's stuff, you can 
> make it static. It's a function then and the delegate weirdness 
> doesn't apply.
>

yeah... no :)
the function inside the main has to have access to the main 
stuff. Its the pointer inside C which could be static, if this 
would help. So long, I go for the enhanced second variant...

> For another approach to your problem, maybe have a look at 
> run-time variadic arguments:
>
> https://dlang.org/spec/function.html#d_style_variadic_functions
>
> With that kind of variadics, you're not dealing with a 
> template. A (run-time) variadic delegate is an actual delegate, 
> i.e. a value that can be passed around. But the variadic stuff 
> is a bit weird to use, and probably affects performance.

Hm... You are probably right... and variadic functions do not 
really match the idea...


More information about the Digitalmars-d-learn mailing list