Thread overview
[Issue 865] New: in overloaded-function, class A should matches (Object) better than (void*)
Jan 21, 2007
d-bugmail
Jan 21, 2007
d-bugmail
Jan 21, 2007
d-bugmail
Apr 24, 2008
d-bugmail
Nov 08, 2008
d-bugmail
January 21, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=865

           Summary: in overloaded-function, class A should matches (Object)
                    better than (void*)
           Product: D
           Version: 1.00
          Platform: PC
               URL: http://www.digitalmars.com/pnews/read.php?server=news.di
                    gitalmars.com&group=digitalmars.D&artnum=46810
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla@digitalmars.com
        ReportedBy: someanon@yahoo.com


From digitalmars.D:

-- class A, matches (Object) better than (void*), so just do the Right thing
to choose overloaded-function(Object obj).

====================================
$ cat ts.d
import std.string;

class A{}

struct B{
  char[] toString()  {return "B";}
}

int c;

char[] toStringFunc(Object obj) {return obj.toString();}
char[] toStringFunc(int i)      {return format(i);}
char[] toStringFunc(void*  obj) {return "null";}

class S(T) {
  T obj;

char[] toString() {
  return toStringFunc(obj);
}

}

int main(char[][] args) {
  S!(A)   sa;
  S!(B*)  sb;
  S!(int) sc;

  printf("%.*s", sa.toString());
  printf("%.*s", sb.toString());
  printf("%.*s", sc.toString());

  return 0;
}

$ dmd.exe ts.d
ts.d(19): function ts.toStringFunc called with argument types:
        (A)
matches both:
        ts.toStringFunc(Object)
and:
        ts.toStringFunc(void*)
ts.d(25): template instance ts.S!(A) error instantiating
====================================

Come on! class A matches both (Object) and (void*)?!


-- 

January 21, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=865


bugzilla@digitalmars.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID




------- Comment #1 from bugzilla@digitalmars.com  2007-01-21 02:48 -------
D has 3 kinds of overload matching:

1) exact match
2) match with implicit conversions
3) no match

There is no dividing up (2) into better or worse matches (like C++). So, the
example is behaving as defined.


-- 

January 21, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=865


someanon@yahoo.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement
             Status|RESOLVED                    |REOPENED
         Resolution|INVALID                     |




------- Comment #2 from someanon@yahoo.com  2007-01-21 12:41 -------
But it certainly makes more sense to match super-class Object.  Can this be considered a feature enhancement?


-- 

April 24, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=865


htvennik@zonnet.nl changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |htvennik@zonnet.nl




------- Comment #3 from htvennik@zonnet.nl  2008-04-24 10:17 -------
I think this is very bad indeed...

The best way to go would be to not allow implicit conversions from a class object to void *. That would not break the rules as mentioned by Walter in reply #1.


-- 

November 08, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=865


gide@nwawudu.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|REOPENED                    |RESOLVED
         Resolution|                            |WORKSFORME




------- Comment #4 from gide@nwawudu.com  2008-11-08 13:15 -------
The example given compiles on DMD 1.033.

C:> cat ts.d
import std.string;
import std.stdio;

class A{}

struct B{
  char[] toString()  {return "B";}
}

int c;

char[] toStringFunc(Object obj) {return obj.toString();}
char[] toStringFunc(int i)      {return format(i);}
char[] toStringFunc(void*  obj) {return "null";}

class S(T) {
  T obj;

char[] toString() {
  return toStringFunc(obj);
}

}

int main(char[][] args) {
  S!(A)   sa;
  S!(B*)  sb;
  S!(int) sc;

  writefln("%.*s", sa.toString());
  writefln("%.*s", sb.toString());
  writefln("%.*s", sc.toString());

  return 0;
}


--