digitalmars.D.learn - Instantiate!(Template, args) in Phobos?
- Nick Treleaven (17/17) Apr 21 2016 Hi,
- Steven Schveighoffer (7/24) Apr 21 2016 This doesn't work?
- Nick Treleaven (8/13) Apr 22 2016 No, nor using the module dot prefix `= .staticEx!(...).staticEx` either:
- Steven Schveighoffer (11/25) Apr 22 2016 OK, I get it. I think this used to work, but I think D has long since
- Nick Treleaven (2/10) Apr 22 2016 OK, great :-)
- David Nadlinger (21/25) Apr 22 2016 From std.meta:
- Nick Treleaven (3/20) Apr 25 2016 Ah, maybe I'd even seen this in passing and forgot. Good point
- Nick Treleaven (2/6) Apr 25 2016 https://github.com/dlang/phobos/pull/4234
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?
Apr 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
Apr 21 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 instantiatingI 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.
Apr 22 2016
On 4/22/16 6:50 AM, Nick Treleaven wrote:On 21/04/2016 18:03, 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.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 instantiatingI didn't realize what the issue was. -SteveI 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.
Apr 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 :-)
Apr 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
Apr 22 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.
Apr 25 2016
On 25/04/2016 11:16, Nick Treleaven wrote:On Friday, 22 April 2016 at 19:09:40 UTC, David Nadlinger wrote:https://github.com/dlang/phobos/pull/4234alias 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.
Apr 25 2016