template alias that includes a parameter
Timoses
timosesu at gmail.com
Sat Jun 30 21:56:11 UTC 2018
On Saturday, 30 June 2018 at 21:11:54 UTC, Anonymouse wrote:
> I have a template that I want to provide easy aliases for,
> where the aliases includes (partially applies?) a template
> parameter.
>
>
> void fooImpl(char token, T)(const T line)
> {
> // ...
> }
>
> alias quoteFoo(T) = fooImpl!('"', T);
would be the same as
template quoteFoo(T)
{
alias quoteFoo = fooImpl!('"', T);
}
There is no "Implicit Function Template Instantiation (IFTI)"
(https://dlang.org/spec/template.html#function-templates) here,
as that only works for function templates. That means the
compiler won't deduce the template parameter type. So that alias
would have to be called with
quoteFoo!string("I'm a quoted sentence.");
> alias singlequoteFoo(T) = fooImpl!('\'', T);
>
> void main()
> {
> quoteFoo(`"asdf"`);
> singlequoteFoo(`'asdf'`);
> }
>
>
> ...was how I'd imagined it would look.
>
>>onlineapp.d(11): Error: template onlineapp.quoteFoo cannot
>>deduce function from argument types !()(string), candidates are:
>>onlineapp.d(6): onlineapp.quoteFoo(T)
>
> If I manually pass string as a template parameter, like
> quoteFoo!string("bar") or singlequoteFoo!string("baz"), it
> works.
>
> Can I do it this way or do I need to write wrapping functions?
Instead of using aliases you could instantiate the function and
use a function pointer to it (I hope my lingo is correct...).
auto quoteFooString = &fooImpl!('"', string);
and use it with
quoteFooString("I'm another quoted sentence.");
I'm trying to find a way to partially apply something to the
template like
//alias quoteFoo = ApplyLeft!(fooImpl, '"', AliasSeq!(int));
//quoteFoo(3);
so the template fooImpl would only be partly instantiated with a
'"', but the second argument would be left for when calling the
function.. Unfortunately, above yields the error
std/meta.d(1232): Error: template instance `Template!'"'`
does not match template declaration fooImpl(char token, T)(const
T line)
... Hm..
https://run.dlang.io/is/di8zjl
import std.stdio;
void fooImpl(char token, T)(const T line)
{
writefln("%s%s%s", token, line, token);
}
auto quoteFooString = &fooImpl!('"', string);
auto singlequoteFooString = &fooImpl!('\'', string);
void main()
{
quoteFooString("test quote");
singlequoteFooString("test single quote");
import std.meta;
// std/meta.d(1232): Error: template instance
`Template!'"'` does not match template
//alias quoteFoo = ApplyLeft!(fooImpl, '"');
//quoteFoo(3);
}
More information about the Digitalmars-d-learn
mailing list