digitalmars.D.learn - Function constraint vs const parameter?
- Jonathan Crapuchettes (13/13) Jun 06 2012 I'm running into a problem with the following function definition when p...
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (32/45) Jun 06 2012 Sorry for being terse but this works:
- Jonathan Crapuchettes (4/55) Jun 07 2012 That worked great! I wish there was a simpler solution, but thank you ve...
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (5/15) Jun 07 2012 I am glad. :) Note that isSomeStringArray should have been named
I'm running into a problem with the following function definition when passing in a const(string[]). public T condenseCountyList(T)(const T inputList) if (is(Unqual!T : string) || is(Unqual!T : string[])) I'm getting the "Error: template common.condenseCountyList does not match any function template declaration" when calling the function as condenseCountyList(countyList); , but if I changed the calling code to condenseCountyList(cast(string[])countyList) dmd is happy. How do I need to change the function constraint to make this work? Thank you, Jonathan Crapuchttes
Jun 06 2012
On 06/06/2012 10:02 AM, Jonathan Crapuchettes wrote:I'm running into a problem with the following function definition when passing in a const(string[]). public T condenseCountyList(T)(const T inputList) if (is(Unqual!T : string) || is(Unqual!T : string[])) I'm getting the "Error: template common.condenseCountyList does not match any function template declaration" when calling the function as condenseCountyList(countyList); , but if I changed the calling code to condenseCountyList(cast(string[])countyList) dmd is happy. How do I need to change the function constraint to make this work? Thank you, Jonathan CrapuchttesSorry for being terse but this works: import std.traits; import std.array; template isSomeStringArray(T) { enum isSomeStringArray = is (typeof( { T variable; static assert (isSomeString!(typeof(variable.front))); }())); } T condenseCountyList(T)(const T inputList) if (isSomeString!T || isSomeStringArray!T) { return T.init; } void main() { auto countyList = [ "Santa Clara" ]; condenseCountyList(countyList); } isSomeString is defined in std.traits. isSomeStringArray above uses a number of D features: * anonymous delegate * executing that delegate by () (which is actually never executed) * typeof, which produces "invalid type" if the expression illegal * is, which produces true if the type that it receives is not invalid * eponymous templates Ali -- D Programming Language Tutorial: http://ddili.org/ders/d.en/index.html
Jun 06 2012
That worked great! I wish there was a simpler solution, but thank you very much
for your help.
JC
Ali Çehreli wrote:
On 06/06/2012 10:02 AM, Jonathan Crapuchettes wrote:
> I'm running into a problem with the following function definition when
> passing in a const(string[]).
>
> public T condenseCountyList(T)(const T inputList) if (is(Unqual!T :
> string) || is(Unqual!T : string[]))
>
> I'm getting the "Error: template common.condenseCountyList does not
> match any function template declaration" when calling the function as
>
> condenseCountyList(countyList);
>
> , but if I changed the calling code to
>
> condenseCountyList(cast(string[])countyList)
>
> dmd is happy.
>
> How do I need to change the function constraint to make this work?
>
> Thank you,
> Jonathan Crapuchttes
Sorry for being terse but this works:
import std.traits;
import std.array;
template isSomeStringArray(T)
{
enum isSomeStringArray = is (typeof(
{
T variable;
static assert (isSomeString!(typeof(variable.front)));
}()));
}
T condenseCountyList(T)(const T inputList)
if (isSomeString!T || isSomeStringArray!T)
{
return T.init;
}
void main()
{
auto countyList = [ "Santa Clara" ];
condenseCountyList(countyList);
}
isSomeString is defined in std.traits. isSomeStringArray above uses a number of
D features:
* anonymous delegate
* executing that delegate by () (which is actually never executed)
* typeof, which produces "invalid type" if the expression illegal
* is, which produces true if the type that it receives is not invalid
* eponymous templates
Ali
Jun 07 2012
On 06/07/2012 12:02 PM, Jonathan Crapuchettes wrote:That worked great! I wish there was a simpler solution, but thank you very much for your help.I am glad. :) Note that isSomeStringArray should have been named isSomeStringRange because it ensures that variable.front compiles. Since slices are ranges, it still works in your case.Alitemplate isSomeStringArray(T) { enum isSomeStringArray = is (typeof( { T variable; static assert (isSomeString!(typeof(variable.front))); }())); }
Jun 07 2012








=?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com>