www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Combining template parameters deduction with default template

reply "Uranuz" <neuranuz gmail.com> writes:
I have a question how I could combine template parameters 
deduction with setting default values for template parameters. I 
will start directly from a piece of code.

string decodeURICustom(string allowedSpecChars = null, bool 
formEncoding = false, T)(T source) pure
{
    //Some processing
}

I want parameter T be deduced from context (T will be string, 
dstring, etc)
And then I want to add some aliases with specialised arguments.

alias decodeURICustom!("!$&'()*+,;=") decodeURIHost;
alias encodeURICustom!("!$&'()*+,;=") encodeURIHost;

alias decodeURICustom!("!$&'()*+,;=: /") decodeURIPath;
alias encodeURICustom!("!$&'()*+,;=: /") encodeURIPath;

But when using decodeURICustom template directly I want to be 
able not set template arguments.

void main()
{
   string str = "http://www.dlang.org";
   string result = decodeURICustom(str);
}

How could I do this? Is it possible. The example above doesn't 
complie.

Of course I could create another template function.

string decodeURIHost(T)(T source)
{

}
Mar 23 2014
next sibling parent "Uranuz" <neuranuz gmail.com> writes:
 string decodeURIHost(T)(T source)
 {

 }
But I like it less and I still don't know how to combine template params deduction with setting second param. string decodeURIHost(T, bool formEncoding)(T source) {}
Mar 23 2014
prev sibling parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 3/23/14, Uranuz <neuranuz gmail.com> wrote:
 I have a question how I could combine template parameters
 deduction with setting default values for template parameters. I
 will start directly from a piece of code.
You can use eponymous templates for this. E.g.: ----- template decodeURICustom(string allowedSpecChars = null, bool formEncoding = false) { string decodeURICustom(T)(T source) pure { return ""; } } alias decodeURICustom!("!$&'()*+,;=") decodeURIHost; void main() { string str = "http://www.dlang.org"; string result = decodeURICustom(str); } -----
Mar 23 2014
parent reply "Uranuz" <neuranuz gmail.com> writes:
On Sunday, 23 March 2014 at 11:15:13 UTC, Andrej Mitrovic wrote:
 On 3/23/14, Uranuz <neuranuz gmail.com> wrote:
 I have a question how I could combine template parameters
 deduction with setting default values for template parameters. 
 I
 will start directly from a piece of code.
You can use eponymous templates for this. E.g.: ----- template decodeURICustom(string allowedSpecChars = null, bool formEncoding = false) { string decodeURICustom(T)(T source) pure { return ""; } } alias decodeURICustom!("!$&'()*+,;=") decodeURIHost; void main() { string str = "http://www.dlang.org"; string result = decodeURICustom(str); } -----
Yes. I like it more that ivoking one function from another. But this trick not working in DMD 2.063 as I understand.
Mar 23 2014
parent reply Philippe Sigaud <philippe.sigaud gmail.com> writes:
On Sun, Mar 23, 2014 at 12:23 PM, Uranuz <neuranuz gmail.com> wrote:

 Yes. I like it more that ivoking one function from another. But this trick
 not working in DMD 2.063 as I understand.
Is there any reason to still use 2.063 (honest question)? The current one is 2.065 with .066 in alpha state. .063 is almost a year old.
Mar 23 2014
parent "Uranuz" <neuranuz gmail.com> writes:
On Sunday, 23 March 2014 at 12:43:35 UTC, Philippe Sigaud wrote:
 On Sun, Mar 23, 2014 at 12:23 PM, Uranuz <neuranuz gmail.com> 
 wrote:

 Yes. I like it more that ivoking one function from another. 
 But this trick
 not working in DMD 2.063 as I understand.
Is there any reason to still use 2.063 (honest question)? The current one is 2.065 with .066 in alpha state. .063 is almost a year old.
No reason. I just had ability to check it in .063 and noticed this fact. Now I use .064 because of some breaking changes in std.json. May be there will be some minor release to fix this (I hope). So I checked only .063, .064.
Mar 23 2014