www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 865] New: in overloaded-function, class A should matches (Object) better than (void*)

reply d-bugmail puremagic.com writes:
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*)?!


-- 
Jan 20 2007
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=865


bugzilla digitalmars.com changed:

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





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.


-- 
Jan 21 2007
prev sibling next sibling parent d-bugmail puremagic.com writes:
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                     |





But it certainly makes more sense to match super-class Object.  Can this be
considered a feature enhancement?


-- 
Jan 21 2007
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=865


htvennik zonnet.nl changed:

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





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



-- 
Apr 24 2008
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=865


gide nwawudu.com changed:

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





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;
}


-- 
Nov 08 2008