Thread overview
Instantiate!(Template, args) in Phobos?
Apr 21, 2016
Nick Treleaven
Apr 22, 2016
Nick Treleaven
Apr 22, 2016
Nick Treleaven
Apr 22, 2016
David Nadlinger
Apr 25, 2016
Nick Treleaven
Apr 25, 2016
Nick Treleaven
April 21, 2016
Hi,
There doesn't seem to be something like this in Phobos:

alias Instantiate(alias Template, T...) = Template!T;

Here's an example of why I need it:

alias staticEx(string msg, string file = __FILE__, size_t line = __LINE__) =
	Instantiate!(.staticEx!(Exception, msg), file, line);

template staticEx(T:Throwable, args...)
{
	auto staticEx(string file = __FILE__, size_t line = __LINE__)()
	...

Full code:
http://dpaste.dzfl.pl/810e55b1acd76

I found std.meta.ApplyLeft but it doesn't seem to work here. I've needed this before and ended up doing a workaround with a template block and temporary alias but it might be nice if Phobos had this. Or is there a simpler solution?
April 21, 2016
On 4/21/16 10:47 AM, Nick Treleaven wrote:
> Hi,
> There doesn't seem to be something like this in Phobos:
>
> alias Instantiate(alias Template, T...) = Template!T;
>
> Here's an example of why I need it:
>
> alias staticEx(string msg, string file = __FILE__, size_t line =
> __LINE__) =
>      Instantiate!(.staticEx!(Exception, msg), file, line);
>
> template staticEx(T:Throwable, args...)
> {
>      auto staticEx(string file = __FILE__, size_t line = __LINE__)()
>      ...
>
> Full code:
> http://dpaste.dzfl.pl/810e55b1acd76
>
> I found std.meta.ApplyLeft but it doesn't seem to work here. I've needed
> this before and ended up doing a workaround with a template block and
> temporary alias but it might be nice if Phobos had this. Or is there a
> simpler solution?

This doesn't work?

alias staticEx(string msg, string file = __FILE__, size_t line = __LINE__) = staticEx!(Exception, msg).staticEx!(file, line);

I would think something with AliasSeq could come in handy, it's the way to alias a compile-time list.

-Steve
April 22, 2016
On 21/04/2016 18:03, Steven Schveighoffer wrote:
> This doesn't work?
>
> alias staticEx(string msg, string file = __FILE__, size_t line =
> __LINE__) = staticEx!(Exception, msg).staticEx!(file, line);

No, nor using the module dot prefix `= .staticEx!(...).staticEx` either:

staticex.d(3): Error: template identifier 'staticEx' is not a member of template 'staticex.staticEx!(Exception, "Look ma, @nogc exception!").staticEx(string file = __FILE__, uint line = __LINE__)()'
staticex.d(20): Error: template instance staticex.staticEx!("Look ma, @nogc exception!", "staticex.d", 20u) error instantiating

> I would think something with AliasSeq could come in handy, it's the way
> to alias a compile-time list.

Not sure how that would help.
April 22, 2016
On 4/22/16 6:50 AM, Nick Treleaven wrote:
> On 21/04/2016 18:03, Steven Schveighoffer wrote:
>> This doesn't work?
>>
>> alias staticEx(string msg, string file = __FILE__, size_t line =
>> __LINE__) = staticEx!(Exception, msg).staticEx!(file, line);
>
> No, nor using the module dot prefix `= .staticEx!(...).staticEx` either:
>
> staticex.d(3): Error: template identifier 'staticEx' is not a member of
> template 'staticex.staticEx!(Exception, "Look ma, @nogc
> exception!").staticEx(string file = __FILE__, uint line = __LINE__)()'
> staticex.d(20): Error: template instance staticex.staticEx!("Look ma,
> @nogc exception!", "staticex.d", 20u) error instantiating

OK, I get it. I think this used to work, but I think D has long since disallowed direct access to eponymous members.

So what you really want to do is:

staticEx!(Exception, msg)!(file, line), but of course this doesn't follow proper grammar.

I think you are right, this cannot be done without such a template. It brings back access to the eponymous nested template, interesting. I think we should add it.

>> I would think something with AliasSeq could come in handy, it's the way
>> to alias a compile-time list.
>
> Not sure how that would help.

I didn't realize what the issue was.

-Steve
April 22, 2016
On 22/04/2016 14:40, Steven Schveighoffer wrote:
> OK, I get it. I think this used to work, but I think D has long since
> disallowed direct access to eponymous members.
>
> So what you really want to do is:
>
> staticEx!(Exception, msg)!(file, line), but of course this doesn't
> follow proper grammar.
>
> I think you are right, this cannot be done without such a template. It
> brings back access to the eponymous nested template, interesting. I
> think we should add it.

OK, great :-)
April 22, 2016
On Thursday, 21 April 2016 at 14:47:55 UTC, Nick Treleaven wrote:
> I found std.meta.ApplyLeft but it doesn't seem to work here. I've needed this before and ended up doing a workaround with a template block and temporary alias but it might be nice if Phobos had this. Or is there a simpler solution?

From std.meta:

---
/*
 * Instantiates the given template with the given list of parameters.
 *
 * Used to work around syntactic limitations of D with regard to instantiating
 * a template from an alias sequence (e.g. T[0]!(...) is not valid) or a template
 * returning another template (e.g. Foo!(Bar)!(Baz) is not allowed).
 */
// TODO: Consider publicly exposing this, maybe even if only for better
// understandability of error messages.
alias Instantiate(alias Template, Params...) = Template!Params;
---

;)

 — David
April 25, 2016
On Friday, 22 April 2016 at 19:09:40 UTC, David Nadlinger wrote:
> From std.meta:
>
> ---
> /*
>  * Instantiates the given template with the given list of parameters.
>  *
>  * Used to work around syntactic limitations of D with regard to instantiating
>  * a template from an alias sequence (e.g. T[0]!(...) is not valid) or a template
>  * returning another template (e.g. Foo!(Bar)!(Baz) is not allowed).
>  */
> // TODO: Consider publicly exposing this, maybe even if only for better
> // understandability of error messages.
> alias Instantiate(alias Template, Params...) = Template!Params;

Ah, maybe I'd even seen this in passing and forgot. Good point about aliasSeq template elements, let's make this public.
April 25, 2016
On 25/04/2016 11:16, Nick Treleaven wrote:
> On Friday, 22 April 2016 at 19:09:40 UTC, David Nadlinger wrote:
>> alias Instantiate(alias Template, Params...) = Template!Params;
>
> Ah, maybe I'd even seen this in passing and forgot. Good point about
> aliasSeq template elements, let's make this public.

https://github.com/dlang/phobos/pull/4234