On 14-03-2012 19:16, H. S. Teoh wrote:
On Wed, Mar 14, 2012 at 07:00:35PM +0100, Alex Rønne Petersen wrote:
On 14-03-2012 19:00, H. S. Teoh wrote:
Code:
        import std.stdio;
        version(explicit) {
                void func(dstring s) {
                        dstring t = s;
                        writeln(t);
                }
        } else {
                void func(S)(S s) {
                        dstring t = s;  // line 10
                        writeln(t);
                }
        }
        void main() {
                func("abc");
        }

If version=explicit is set, the program compiles fine. But if not, then
the compiler complains:

        test.d:10: Error: cannot implicitly convert expression (s) of type 
string to immutable(dchar)[]

What do I need to do to make the template version of func trigger
implicit conversion of the string lit to dstring? What I'm trying to do
is to templatize dstring as well, but still benefit from the
string->dstring conversion when instantiated with dstring.

(Ditto with implicit conversion to wstring.)


T


I doubt that such an implicit conversion even exists. You have to
prefix the string appropriately, e.g.:

OK, maybe implicit conversion is the wrong word. The compiler is
obviously interpreting func("abc") as func("abc"d) when we declare
func(dstring). But when we declare func(S)(S), the compiler deduces
"abc" as string and sets S=string.

What I want is to force the compiler to deduce S=dstring when I declare
func(S)(S) and call it as func("abc").


T


But then... why not just make it take a dstring? Maybe I'm not following your intent here...

Anyway, what Phobos functions tend to do is to set S = string by default and otherwise force people to either pass the string type *or* use the string suffices.

--
- Alex

Reply via email to