Thread overview
[Issue 4875] New: Allow struct initialization with constructor
Sep 15, 2010
Sobirari Muhomori
Sep 15, 2010
Sobirari Muhomori
Sep 16, 2010
Sobirari Muhomori
Jan 24, 2012
Walter Bright
September 15, 2010
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


--- Comment #0 from Sobirari Muhomori <dfj1esp02@sneakemail.com> 2010-09-15 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: -------
September 15, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4875


bearophile_hugs@eml.cc changed:

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


--- Comment #1 from bearophile_hugs@eml.cc 2010-09-15 14:49:30 PDT ---
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: -------
September 15, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4875



--- Comment #2 from Sobirari Muhomori <dfj1esp02@sneakemail.com> 2010-09-15 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: -------
September 15, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4875



--- Comment #3 from bearophile_hugs@eml.cc 2010-09-15 16:18:13 PDT ---
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: -------
September 16, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4875



--- Comment #4 from Sobirari Muhomori <dfj1esp02@sneakemail.com> 2010-09-16 15:36:08 PDT ---
(In reply to comment #1)
> 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: -------
January 24, 2012
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


--- Comment #5 from Walter Bright <bugzilla@digitalmars.com> 2012-01-23 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: -------