Question about function templates when parameter is void
Gary Willoughby
dev at nomad.so
Sat Nov 9 07:48:17 PST 2013
On Saturday, 9 November 2013 at 07:11:50 UTC, Uranuz wrote:
> I have a question. In my code I want to make uniform template
> function to call JSON RPC methods (that are regular D functions
> of any type). So I need some way to call this function with no
> function params and with them in the same manner. There is a
> concrete example, because this description isn't obvious (I
> think that)
>
> //----------------
> import std.stdio, std.json, std.conv, std.traits;
>
>
> JSONValue getStdJSON(T)(T dValue)
> {
> static if( is( T == void ) )
> {
> JSONValue result;
> result.type = JSON_Type.NULL;
> return result;
> }
> else
> {
> //....
> }
>
> }
>
> void f()
> {
> writeln("Hello!!");
> }
>
> void main()
> {
> writeln(getStdJSON(f()));
> }
> //------------------
> Compilation output:
> /d661/f210.d(28): Error: template f210.getStdJSON does not
> match any function template declaration. Candidates are:
> /d661/f210.d(6): f210.getStdJSON(T)(T dValue)
> /d661/f210.d(28): Error: template f210.getStdJSON(T)(T dValue)
> cannot deduce template function from argument types !()(void)
>
> //---------------
>
> So my question is how can I implement something like this?
> Problem is that we don't really have value of type void, but I
> want uniform way to call this function when it has parameter
> with concrete type and not.
It seems odd to call a function as an argument which returns
void. Surely you could just move that out and call it alone
before the 'getStdJSON' call?
Anyway to answer you question, i'd probably overload the
'getStdJSON' function.
More information about the Digitalmars-d-learn
mailing list