digitalmars.D - Extracting string parameter from template instance received via alias
- MrSmith (43/43) Sep 12 2014 Given the following program:
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (10/25) Sep 12 2014 TemplateArgsOf:
- MrSmith (2/31) Sep 12 2014 Thank you, Ali. I will try to not mismatch open tabs next time =)
- anonymous (27/70) Sep 12 2014 `is(...)` checks for types. firstInstance isn't a type, it's a
Given the following program:
---------------------------------
import std.stdio;
template first(string s)
{
string first(string par)
{
if (par == s)
return "true";
else
return "false";
}
}
template second(alias firstInstance)
{
string second(string par)
{
// this line doesn't work
static if (is(firstInstance : F!(str), alias F, string str))
{
writeln("matched ", str);
}
enum s = "XXX"; // get s from firstInstance
// without parsing strings and using mixins
// something like second(alias C : F!(str), alias F, string str)
import std.string : icmp;
if (icmp(par, s) == 0)
return "true";
else
return "false";
}
}
void main()
{
writeln(first!"str"("str"));
writeln(second!( first!"str" )("StR")); // should match string
from first, but case insensetive
}
---------------------------------
How do I extract s parameter of first passed to second via alias
parameter. It seems like it is not possible. Or is it?
Sep 12 2014
On 09/12/2014 12:44 PM, MrSmith wrote:
Given the following program:
---------------------------------
import std.stdio;
template first(string s)
{
string first(string par)
{
if (par == s)
return "true";
else
return "false";
}
}
template second(alias firstInstance)
{
TemplateArgsOf:
import std.traits;
foreach (i, arg; TemplateArgsOf!firstInstance) {
writefln("arg %s: %s", i, arg);
}
Prints:
arg 0: str
Ali
P.S. We want to see these topics over at the D.learn newsgroup. ;)
Sep 12 2014
On Friday, 12 September 2014 at 20:37:44 UTC, Ali Çehreli wrote:On 09/12/2014 12:44 PM, MrSmith wrote:Thank you, Ali. I will try to not mismatch open tabs next time =)Given the following program: --------------------------------- import std.stdio; template first(string s) { string first(string par) { if (par == s) return "true"; else return "false"; } } template second(alias firstInstance) {TemplateArgsOf: import std.traits; foreach (i, arg; TemplateArgsOf!firstInstance) { writefln("arg %s: %s", i, arg); } Prints: arg 0: str Ali P.S. We want to see these topics over at the D.learn newsgroup. ;)
Sep 12 2014
On Friday, 12 September 2014 at 19:44:28 UTC, MrSmith wrote:Given the following program: --------------------------------- import std.stdio; template first(string s) { string first(string par) { if (par == s) return "true"; else return "false"; } } template second(alias firstInstance) { string second(string par) { // this line doesn't work static if (is(firstInstance : F!(str), alias F, string str))`is(...)` checks for types. firstInstance isn't a type, it's a function, so the `is` expression is always false. You can use std.traits.TemplateArgsOf instead: import std.traits; import std.typetuple; static if(__traits(compiles, TemplateArgsOf!firstInstance)) { alias targs = TemplateArgsOf!firstInstance; static if(is(typeof(targs) == TypeTuple!string)) writeln("matched ", targs[0]); }{ writeln("matched ", str); } enum s = "XXX"; // get s from firstInstance // without parsing strings and using mixins // something like second(alias C : F!(str), alias F, string str)You're on the right track: template second(alias C : F!(str), alias F, string str) { string second(string par) { import std.string : icmp; if (icmp(par, str) == 0) return "true"; else return "false"; } }import std.string : icmp; if (icmp(par, s) == 0) return "true"; else return "false"; } } void main() { writeln(first!"str"("str")); writeln(second!( first!"str" )("StR")); // should match string from first, but case insensetive } --------------------------------- How do I extract s parameter of first passed to second via alias parameter. It seems like it is not possible. Or is it?
Sep 12 2014









"MrSmith" <mrsmith33 yandex.ru> 