std.path review: update

Jonathan M Davis jmdavisProg at gmx.com
Mon Jul 18 09:18:39 PDT 2011


On Monday 18 July 2011 06:28:50 bearophile wrote:
> Jonathan M Davis:
> > And it _should_ be a template. All of the stuff like that are templates.
> > And I'm not even sure that it _can_ be a function. And even if it can,
> > what would we gain by making it a function anyway? It's operating on
> > types. It's of no use at runtime. It's a perfect candidate for an
> > eponymous template. std.traits, std.range, etc. do this sort of thing
> > in pretty much exactly the same way. There may be a cleaner way to
> > write it then it currently is, but using an eponymous template like
> > that is the correct thing to do.
> 
> This seems to work:
> 
> 
> import std.traits: isSomeChar, Unqual, isSomeString;
> 
> bool compatibleStrings(Strings...)() if (Strings.length) {
>     static if (isSomeString!(Strings[0])) {
>         alias Unqual!(typeof(Strings[0].init[0])) TC;
>         foreach (s; Strings[1 .. $])
>             static if (isSomeString!s && !is(TC ==
> Unqual!(typeof(s.init[0])))) return false;
>         return true;
>     } else
>         return false;
> }
> 
> version (unittest) {
>     static assert (compatibleStrings!(char[], const(char)[], string)());
>     static assert (compatibleStrings!(wchar[], const(wchar)[], wstring)());
>     static assert (compatibleStrings!(dchar[], const(dchar)[], dstring)());
>     static assert (!compatibleStrings!(int[], const(int)[],
> immutable(int)[])()); static assert (!compatibleStrings!(char[],
> wchar[])());
>     static assert (!compatibleStrings!(char[], dstring)());
> }
> 
> void main() {}
> 
> I have written tons of such things in dlibs1, and generally I have seen that
> recursive templates are slower and need more RAM than similar functions.

Okay. Yes, you could do that. But what you're doing is basically the same as 
the eponymous template except that it's saving the value to in a function so 
that it can be called at runtime. The gain is 0 and potentially confusing. 
It's no better than

bool compatibleStringsFunc(Strings...)()
{
	enum retval = compatibleStrings!Strings;
	return retval;
}

But you _did_ find a way to turn it into a function.

- Jonathan M Davis


More information about the Digitalmars-d mailing list