www.digitalmars.com         C & C++   DMDScript  
Archives

D Programming
digitalmars.D
digitalmars.D.bugs
digitalmars.D.dtl
digitalmars.D.ide
digitalmars.D.dwt
digitalmars.D.announce
digitalmars.D.learn
digitalmars.D.debugger
D.gnu
D

C/C++ Programming
c++
c++.announce
c++.atl
c++.beta
c++.chat
c++.command-line
c++.dos
c++.dos.16-bits
c++.dos.32-bits
c++.idde
c++.mfc
c++.rtl
c++.stl
c++.stl.hp
c++.stl.port
c++.stl.sgi
c++.stlsoft
c++.windows
c++.windows.16-bits
c++.windows.32-bits
c++.wxwindows

digitalmars.empire
digitalmars.DMDScript
electronics


digitalmars.D.bugs - [Issue 3070] New: Implicitly conversion on function call

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

           Summary: Implicitly conversion on function call
           Product: D
           Version: 2.030
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Severity: trivial
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: k0l0b0k.void gmail.com


I have next pieces of code:

struct MyString
{
    public MyString opAssign(string pStr)
    {
        throw new Error("");
    }

    public MyString opCast(string pStr)
    {
        throw new Error("");
    }

    public MyString opImplicitCast(string pStr)
    {
        throw new Error("");
    }

    this(string pStr)
    {
        throw new Error("");
    }
}

void foo(MyString pStr)
{
}

int main(char[][] args)
{
    MyString str = "test1";
    str = "test2";
    foo(str);
    foo("test3");        // error "Error: cannot implicitly convert expression
("test3") of type immutable(char)[] to MyString
    return 0;
}


This is just sample. So, my trouble is to pass string argument to foo() with
implicit conversion to MyString struct (I also tried class, with no results).
I'm googled by this topic, but nothing found. Can you help me? This is not a
bug (I think), but the question - how I can implicitly convert from one object
to another in the as C++ does (C++ will call constructor with parameter type
"string"). How I can make same in D?

Thanks in advance!

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 15 2009
parent Robert Fraser <fraserofthenight gmail.com> writes:
d-bugmail puremagic.com wrote:
 http://d.puremagic.com/issues/show_bug.cgi?id=3070
 
            Summary: Implicitly conversion on function call
            Product: D
            Version: 2.030
           Platform: x86
         OS/Version: Windows
             Status: NEW
           Severity: trivial
           Priority: P2
          Component: DMD
         AssignedTo: nobody puremagic.com
         ReportedBy: k0l0b0k.void gmail.com
 
 
 I have next pieces of code:
 
 struct MyString
 {
     public MyString opAssign(string pStr)
     {
         throw new Error("");
     }
 
     public MyString opCast(string pStr)
     {
         throw new Error("");
     }
 
     public MyString opImplicitCast(string pStr)
     {
         throw new Error("");
     }
 
     this(string pStr)
     {
         throw new Error("");
     }
 }
 
 void foo(MyString pStr)
 {
 }
 
 int main(char[][] args)
 {
     MyString str = "test1";
     str = "test2";
     foo(str);
     foo("test3");        // error "Error: cannot implicitly convert expression
 ("test3") of type immutable(char)[] to MyString
     return 0;
 }
 
 
 This is just sample. So, my trouble is to pass string argument to foo() with
 implicit conversion to MyString struct (I also tried class, with no results).
 I'm googled by this topic, but nothing found. Can you help me? This is not a
 bug (I think), but the question - how I can implicitly convert from one object
 to another in the as C++ does (C++ will call constructor with parameter type
 "string"). How I can make same in D?
 
 Thanks in advance!
 

If you want quicker responses to questions, try the newsgroups or IRC. Bugzilla is usually for bugs :-). Anyway, user-defined implicit casting is not implemented in (any version of) D yet. Sorry!
Jun 15 2009