Re: Template function that accept strings and array of strings

2015-07-17 Thread Jacob Carlborg via Digitalmars-d-learn
On 2015-07-16 18:49, badlink wrote: The method with the variadic function works, but I would have to use only one parameter because this doesn't work: fun(const(char[])[] a, const(char[])[] b ...) and is a bit ugly in my use case ... I don't think I really understand how you want to use/call

Re: Template function that accept strings and array of strings

2015-07-17 Thread badlink via Digitalmars-d-learn
On Friday, 17 July 2015 at 12:58:58 UTC, Jacob Carlborg wrote: I don't think I really understand how you want to use/call the function. Could you give an example with all the different types you want to call the function? My fault, I didn't test the variadic function enough and jumped to

Re: Template function that accept strings and array of strings

2015-07-17 Thread Jacob Carlborg via Digitalmars-d-learn
On 2015-07-17 19:25, badlink wrote: My fault, I didn't test the variadic function enough and jumped to conclusion. It actually works well http://pastebin.com/R4EHuBLh Cool :) Sometimes D developers think templates will be needed to solve everything. -- /Jacob Carlborg

Re: Template function that accept strings and array of strings

2015-07-16 Thread Jacob Carlborg via Digitalmars-d-learn
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

Re: Template function that accept strings and array of strings

2015-07-16 Thread badlink via Digitalmars-d-learn
Thank you for all answers. Removing typeof do resolve the problem when the second parameter is a simple string. However when passing an array of string the error still occur: Error: template cache.MetadataCache.hasItemParent cannot deduce function from argument types !()(string, string[]).

Re: Template function that accept strings and array of strings

2015-07-16 Thread Gary Willoughby via Digitalmars-d-learn
On Wednesday, 15 July 2015 at 21:57:50 UTC, 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

Re: Template function that accept strings and array of strings

2015-07-16 Thread Gary Willoughby via Digitalmars-d-learn
Also checkout inout functions: http://dlang.org/function.html#inout-functions

Re: Template function that accept strings and array of strings

2015-07-16 Thread badlink via Digitalmars-d-learn
After a thorough reading of the documentation I found an even simpler solution: bool hasItemParent(T)(const(char)[] itemId, T parentId) if (is(T : const(char)[]) || is(T : const(char[])[])) { ... } Now it accepts all these: char[], const(char)[], immutable(char)[], char[][], const(char)[][]

Re: Template function that accept strings and array of strings

2015-07-16 Thread badlink via Digitalmars-d-learn
On Thursday, 16 July 2015 at 18:41:47 UTC, Gary Willoughby wrote: bool hasItemParent(A, B)(A itemId, B parentId) if (isSomeString!(A) (isSomeString!(B) || isArray!(B) isSomeString!(ElementType!(B Thank you ! I completely missed isSomeString. I think the definition can be safely

Re: Template function that accept strings and array of strings

2015-07-15 Thread Vlad Levenfeld via Digitalmars-d-learn
On Wednesday, 15 July 2015 at 21:57:50 UTC, 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

Re: Template function that accept strings and array of strings

2015-07-15 Thread Yuxuan Shui via Digitalmars-d-learn
On Wednesday, 15 July 2015 at 21:57:50 UTC, 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

Template function that accept strings and array of strings

2015-07-15 Thread badlink via Digitalmars-d-learn
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