Re: Template specialisation, "Generic type locking", offline stdlib docs and case-based template question
On Friday, December 01, 2017 03:39:12 helxi via Digitalmars-d-learn wrote: > 1. Template specialisation. > Why is this useful?: > T getResponse(T = int)(string question); And how does it differ > from > int getResponse(string question); ? > > Context: http://ddili.org/ders/d.en/templates.html : Section: > "Default template parameters" I would point out that that isn't template specialization at all. Template specialization uses : not =, and would look like T getResponse(T : int)(string question) {...} and is a different beast entirely. As the section you're referring to mentions, what you have there is a default template parameter, which is useful in cases where you want to be able to have the flexibility of a template parameter but not require it in the common case. One prime example is std.algorithm's find function. You can pass it a predicate that it will use for comparing elements in the range, but by default it uses equality. - Jonathan M Davis
Re: Template specialisation, "Generic type locking", offline stdlib docs and case-based template question
On Friday, 1 December 2017 at 03:39:12 UTC, helxi wrote: 1. Template specialisation. Why is this useful?: T getResponse(T = int)(string question); And how does it differ from int getResponse(string question); ? Because you can change the T at the call site. Let me give you a real world example from my cgi.d docs: http://dpldocs.info/experimental-docs/arsd.cgi.Cgi.request.html source: https://github.com/adamdruppe/arsd/blob/master/cgi.d#L2057 In the doc example, you'll see: int a = cgi.request("number", 10); You can also do: string name = cgi.request("name"); or something kinda crazy like: float number = cgi.request!float("brightness"); In the first case, it uses the passed param - 10 - to infer the type of T you want returned. It sees 10 is an int, so it tries to convert the value from the URL to an int and return it. The second case uses the default parameter, described in the link you gave. The default here is (T = string), so if you don't specify anything else, it simply returns a string. The third case shows explicit passing of a new type, instead of using the default T = string, we get T = float, so the function converts the value from URL to float and returns that. All three of these uses are supported by the code. 2. "Generic locking". Is it possible to specialise templates for a certain group of types? For example auto fn(T)(T arg) auto fn(T : int, double, float, ulong)(T arg); //shares same behaviour auto fn(T : char, string, dchar, wchar, dstring, wstring)(T arg); // shares same behavior Yeah, that is possible. class Stack(T) { private: T[] data; public: this(T)(T[] data){ /*..*/} this(T)(){} //... } void main() { auto s = new Stack!int; } Says: Error: template app.Stack!int.Stack.__ctor cannot deduce function from argument types !()(), candidates are: source/app.d(6,2):app.Stack!int.Stack.__ctor(T)(T[] data) source/app.d(9,2):app.Stack!int.Stack.__ctor(T)() Why is Stack!int a ctor()() instead of a ctor(int)()? You passed int to the Stack(T) template, but not to the this(T) template. There's two levels there, the outer argument and the inner argument. It is the same as if you did a void delegate(int) returns_a_function(int); if you called returns_a_function(0), it would still return the delegate. If you want to call both, you need another set of params to pass an argument to the returned function, too.
Re: Template specialisation, "Generic type locking", offline stdlib docs and case-based template question
On Friday, 1 December 2017 at 03:39:12 UTC, helxi wrote: 1. Template specialisation. Why is this useful?: T getResponse(T = int)(string question); And how does it differ from int getResponse(string question); ? Good Q. Without thinking more it looks like a pointless example. The only difference i see is that the templatized version won't get compiled unless used. Context: http://ddili.org/ders/d.en/templates.html : Section: "Default template parameters" 2. "Generic locking". Is it possible to specialise templates for a certain group of types? For example auto fn(T)(T arg) auto fn(T : int, double, float, ulong)(T arg); //shares same behaviour auto fn(T : char, string, dchar, wchar, dstring, wstring)(T arg); // shares same behavior You can use template constraints: auto fn(T)(T arg) if (is(T==int) || is(T==double) || is(T==float) || is(T==ulong)){} auto fn(T)(T arg) if (is(T==char) || is(T==string) || is(T==dchar) || is(T==wchar)){} 3. "Offline docs". Is there any offline documentation of the stdlib? Like https://en.cppreference.com/mwiki/index.php?title=Cppreference:Archives&oldid=95461 Yes, distributes with each release. look inside the 7z archive. On linux it's setup here: /usr/share/dmd/html/d/phobos/ 4. Case based question regarding templates: class Stack(T) { private: T[] data; public: this(T)(T[] data){ /*..*/} this(T)(){} //... } void main() { auto s = new Stack!int; } Says: Error: template app.Stack!int.Stack.__ctor cannot deduce function from argument types !()(), candidates are: source/app.d(6,2):app.Stack!int.Stack.__ctor(T)(T[] data) source/app.d(9,2):app.Stack!int.Stack.__ctor(T)() Why is Stack!int a ctor()() instead of a ctor(int)()? You must use another identifier here class Stack(T) { private: T[] data; public: this(TT)(TT[] data){ /*..*/} this()(){} //... }
Template specialisation, "Generic type locking", offline stdlib docs and case-based template question
1. Template specialisation. Why is this useful?: T getResponse(T = int)(string question); And how does it differ from int getResponse(string question); ? Context: http://ddili.org/ders/d.en/templates.html : Section: "Default template parameters" 2. "Generic locking". Is it possible to specialise templates for a certain group of types? For example auto fn(T)(T arg) auto fn(T : int, double, float, ulong)(T arg); //shares same behaviour auto fn(T : char, string, dchar, wchar, dstring, wstring)(T arg); // shares same behavior 3. "Offline docs". Is there any offline documentation of the stdlib? Like https://en.cppreference.com/mwiki/index.php?title=Cppreference:Archives&oldid=95461 4. Case based question regarding templates: class Stack(T) { private: T[] data; public: this(T)(T[] data){ /*..*/} this(T)(){} //... } void main() { auto s = new Stack!int; } Says: Error: template app.Stack!int.Stack.__ctor cannot deduce function from argument types !()(), candidates are: source/app.d(6,2):app.Stack!int.Stack.__ctor(T)(T[] data) source/app.d(9,2):app.Stack!int.Stack.__ctor(T)() Why is Stack!int a ctor()() instead of a ctor(int)()?