Thread overview
[Issue 3189] New: `std.conv.to` : check for a custom `to` method in classes/structs
Jul 18, 2009
julien@onandon.be
July 18, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3189

           Summary: `std.conv.to` : check for a custom `to` method in
                    classes/structs
           Product: D
           Version: future
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody@puremagic.com
        ReportedBy: julien@onandon.be


Hello,

it would be nice if `std.conv.to` on class or struct could check if this class or struct implements its own `to` method.

Something as :

           module Date;

                      class Date
                      {
                                 T to(T)() if(is(T == long))
                                 {
                                            return timestamp;
                                 }
                      }

           module std.conv;

                      T to(T, S)(S s) if (is(S : Object)
                      {
                                 static if(is(typeof(s.to!(T)())))
                                            return s.to!(T)();
                                 return /* whatever */;
                      }

           module test;

                      void main()
                      {
                                 assert(to!(long)(new Date) == 123124142324);
                      }

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 19, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3189


Andrei Alexandrescu <andrei@metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED




-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 28, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3189


Andrei Alexandrescu <andrei@metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|                            |FIXED




--- Comment #1 from Andrei Alexandrescu <andrei@metalanguage.com>  2009-08-28 08:28:09 PDT ---
Ok. I implemented this:

/**
Object-_to-non-object conversions look for a method "to" of the source
object.

Example:
----
class Date
{
    T to(T)() if(is(T == long))
    {
        return timestamp;
    }
    ...
}

unittest
{
    auto d = new Date;
    auto ts = to!long(d); // same as d.to!long()
}
----
 */
T to(T, S)(S value) if (is(S : Object) && !is(T : Object) && !isSomeString!T
        && is(typeof(S.init.to!(T)()) : T))
{
    return value.to!T();
}

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