www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 3715] New: std.string.format can't use const/immutable toString functions

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3715

           Summary: std.string.format can't use const/immutable toString
                    functions
           Product: D
           Version: 2.036
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody puremagic.com
        ReportedBy: tomeksowi gmail.com



PST ---
Test case:

struct A {
    string toString() const {
        return "A";
    }
}

void main() {
    writeln(format("%s", A()));
}

Outputs:
std.format.FormatError: std.format Can't convert test.A to string: "string
toStr
ing()" not defined

Same for immutable.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 17 2010
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3715


Jonathan M Davis <jmdavisProg gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jmdavisProg gmx.com



PDT ---
nothrow and pure don't work either. My guess is that toString() must have the
_exact_ signature of public string toString() or it doesn't work with format(),
so _no_ other function modifiers can be used on it.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 17 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3715


Steven Schveighoffer <schveiguy yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |schveiguy yahoo.com



10:33:52 PDT ---
Yes, std.string.format uses varargs, not templates, so it can only do what the
TypeInfo allows it to do.  And the compiler will only store the function
pointer to toString if it exactly matches the right signature.

See some related discussion in
http://d.puremagic.com/issues/show_bug.cgi?id=4290

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 18 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3715




PDT ---

 Yes, std.string.format uses varargs, not templates, so it can only do what the
 TypeInfo allows it to do.  And the compiler will only store the function
 pointer to toString if it exactly matches the right signature.
OK, now I understand why it doesn't work, thanks. But how to get it working? In real life toString() will often be const|pure|nothrow because of its nature. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Sep 19 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3715




04:36:35 PDT ---
Well, first of all, toString should always be const.

But pure and nothrow are two options that are also valid.  I think some real
thought needs to go into delegates and function pointers, and what can
implicitly cast.  Right now, there are no implicit casts between function
pointers (except const casts which are invalid anyways).

As a workaround, you can do something like this:

struct A {
  /*pure nothrow?*/ string toString() const { return "A"; }
  string toString() {const(A)*me = &this; return me.toString();}
}

What this does is put a common implementation for each overload so you don't
have to rewrite it.

When the requirements change (and they will, because toString should be const),
then in most cases you can just get away with a const function.  If you want to
have pure and nothrow available, then you still have to do something like this.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 20 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3715




22:11:40 PST ---
 Right now, there are no implicit casts between function
 pointers (except const casts which are invalid anyways).
Looks like this is "fixed", see bug 3797. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Dec 02 2010
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3715


Andrej Mitrovic <andrej.mitrovich gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |andrej.mitrovich gmail.com
         Resolution|                            |FIXED



09:09:38 PST ---
Fixed in 2.060.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 21 2012