www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 4875] New: Allow struct initialization with constructor

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

           Summary: Allow struct initialization with constructor
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: websites
        AssignedTo: nobody puremagic.com
        ReportedBy: dfj1esp02 sneakemail.com



14:45:38 PDT ---
If we have a struct
---
struct HANDLE
{
    size_t Value=-1;
    this(void *ptrValue)
    {
        Value=cast(size_t)ptrValue;
    }
    this(size_t intValue)
    {
        Value=intValue;
    }
}
---
it's possible to initialize variables of type HANDLE with values of types void*
and size_t:
---
HANDLE h1=4, h2=null;
---
But it's impossible to initialize class fields and function arguments in the
same way:
---
class File
{
    HANDLE Handle=5; //cannot implicitly convert expression (5) of type int to
HANDLE
}

void FFFF(HANDLE){}
FFFF(5);
FFFF(null); //cannot implicitly convert expression (null) of type void* to
HANDLE
---
This complicates porting from C because in C it's a common practice to pass
null as a function argument for a handle value.

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


bearophile_hugs eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs eml.cc



You may write this:

class File {
    HANDLE Handle = HANDLE(5);
}

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




15:59:47 PDT ---
The code should be still source-compatible with alias void* HANDLE;

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




Is this good enough?


struct HANDLE {
    size_t value = -1; // variables are lowercase in D

    this(void *ptrValue) {
        value = cast(size_t)ptrValue;
    }
    this(size_t intValue) {
        value = intValue;
    }

    void opAssign(void *ptrValue) {
        value = cast(size_t)ptrValue;
    }
    void opAssign(size_t intValue) {
        value = intValue;
    }
}

// alias size_t HANDLE;

class File {
    HANDLE handle;
    this() {
        handle = 5;
    }
}

void main() {}

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




15:36:08 PDT ---

 You may write this:
 
 class File {
     HANDLE Handle = HANDLE(5);
 }
Hmm... giving it another thought, forcing explicit constructor syntax can be ok: it makes things explicit and with proper OO wrapping doesn't require big changes. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Sep 16 2010
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4875


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla digitalmars.com
         Resolution|                            |WONTFIX



23:04:11 PST ---
Allowing such implicit conversions works in C++, but is considered a defect by
experienced C++ professionals. We won't repeat the mistake.

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