On 2015-07-15 23:57, badlink wrote:
Hello, I can't figure how to write a template function that accept
either strings or array of strings.

This is my current code:

bool hasItemParent(T)(const(char)[] itemId, const(T)[] parentId)
if (is(typeof(T) == char) || (isArray!T && is(typeof(T[]) == char)))
{...}

I used const(T)[] because I'd like to accept immutable and mutable strings.
But calling it with an immutable string generate this error:

Error: template cache.MetadataCache.hasItemParent cannot deduce function
from argument types !()(string, string), candidates are:
cache.MetadataCache.hasItemParent(T)(const(char)[] itemId, const(T)[]
parentId) if (is(typeof(T) == char))

Any suggestions ?

If I understand you correctly, I think you want a type safe variadic function:

void foo (const(char[])[] args ...)
{
    writeln(args);
}

void main()
{
    foo("foo", "bar");
    foo("foo".dup, "bar".dup);
    auto a = ["foo", "bar"];
    foo(a);
    auto b = ["foo".dup, "bar".dup];
    foo(b);
}

--
/Jacob Carlborg

Reply via email to