www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 5460] New: enum of struct not calling constructor

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

           Summary: enum of struct not calling constructor
           Product: D
           Version: D1 & D2
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: andrej.mitrovich gmail.com


--- Comment #0 from Andrej Mitrovic <andrej.mitrovich gmail.com> 2011-01-18
09:36:41 PST ---
Example 1:

import std.stdio;

enum WindowSizes : QSize
{
   Minimum = QSize(10)
}

struct QSize
{
   int store;
   this(int x)
   {
       store = x + 10;
   }
}

void main()
{
   auto foo = WindowSizes.Minimum;
   assert(foo.store == 10);  // What?

   auto bar = QSize(10);
   assert(bar.store == 20);
}

The constructor is never called for the enum. It does field by field
assignments instead, as shown in this case:

import std.stdio;

enum WindowSizes : Inverted
{
   Minimum = Inverted(10, 20)
}

struct Inverted
{
   int x;
   int y;
   this(int in_x, int in_y)
   {
       x = in_y;
       y = in_x;
   }
}

void main()
{
   auto foo = WindowSizes.Minimum;
   assert(foo.x == 10);
   assert(foo.y == 20);

   auto bar = Inverted(10, 20);
   assert(bar.x == 20);
   assert(bar.y == 10);
}

Field assignments should be disabled when a constructor is present.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 18 2011
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5460



--- Comment #1 from Andrej Mitrovic <andrej.mitrovich gmail.com> 2011-01-18
09:38:13 PST ---
*I meant constructing a struct with field assignment*, not regular field
assignment after a struct was constructed.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 18 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5460



--- Comment #2 from Andrej Mitrovic <andrej.mitrovich gmail.com> 2012-01-04
07:33:27 PST ---
The following has now changed into a CT error:

enum Sizes : Size {
    Minimum = Size(10)
}

struct Size {
   int store;
}

void main()
{
}

test.d(4): Error: Integer constant expression expected instead of Size(10)
test.d(4): Error: Integer constant expression expected instead of Size(10)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 04 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5460


Andrej Mitrovic <andrej.mitrovich gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
           Platform|Other                       |All
         Resolution|                            |WORKSFORME
         OS/Version|Windows                     |All


--- Comment #3 from Andrej Mitrovic <andrej.mitrovich gmail.com> 2013-09-17
13:03:30 PDT ---
Since 2.063.2 (likely due to enum fixes as well), this code now works properly.
Gotta give a huge thanks to Don and Kenji (and others) for enabling CTFE to
work great these days.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 17 2013