www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 12346] New: Inheriting from a class with a private constructor results in a runtime error

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

           Summary: Inheriting from a class with a private constructor
                    results in a runtime error
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: major
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: slavo5150 yahoo.com


--- Comment #0 from Mike <slavo5150 yahoo.com> 2014-03-11 04:25:25 PDT ---
/********** test.d ***************/
module test;

abstract class DoNotInstantiate
{
    private this() {}
}

class TestClass : DoNotInstantiate
{
    uint value;
}



/************ main.d **************/
void main()
{
    test.TestClass x; // <---  This should result in a compile-time error
    x.value = 10;     // <---  Runtime error. 
}

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 11 2014
next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12346


Mike <slavo5150 yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|Inheriting from a class     |Instantiating class with a
                   |with a private constructor  |private constructor results
                   |results in a runtime error  |in a runtime error


--- Comment #1 from Mike <slavo5150 yahoo.com> 2014-03-11 04:53:03 PDT ---
Turns out you don't even need inheritance to reproduce the problem.

/********** test.d ***************/
module test;

class TestClass : DoNotInstantiate
{
    private this() {}

    uint value;
}



/************ main.d **************/
void main()
{
    test.TestClass x; // <---  This should result in a compile-time error
    x.value = 10;     // <---  Runtime error. 
}

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 11 2014
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12346



--- Comment #2 from Mike <slavo5150 yahoo.com> 2014-03-11 04:55:40 PDT ---
Last post had an error in the code.  Use this instead.

/********** test.d ***************/
module test;

class TestClass
{
    private this() {}

    uint value;
}



/************ main.d **************/
void main()
{
    test.TestClass x; // <---  This should result in a compile-time error
    x.value = 10;     // <---  Runtime error. 
}

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 11 2014
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12346


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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |andrej.mitrovich gmail.com
         Resolution|                            |INVALID


--- Comment #3 from Andrej Mitrovic <andrej.mitrovich gmail.com> 2014-03-11
05:05:46 PDT ---
1: You're not instantiating the class. You need to use 'new' to instantiate it,
via:

-----
class TestClass
{
    private this() {}

    uint value;
}

void main()
{
    TestClass x = new TestClass();
    x.value = 10;
}
-----

2: This is a reference, not instantiation, it cannot be an error even if the
reference type is of an abstract class:

-----
abstract class AC
{
    private this() {}
}

void main()
{
    AC ac;  // allowed
}
-----

3: Private symbols are visible and usable in the entire module they're defined
in.

Please read the documentation before filing bugs, or use the D forums[1] if you
have questions or want to learn about D.

[1] : http://forum.dlang.org/group/digitalmars.D.learn

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 11 2014
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12346



--- Comment #4 from Mike <slavo5150 yahoo.com> 2014-03-11 17:47:56 PDT ---
How ironic!
http://d.puremagic.com/issues/show_bug.cgi?id=4595

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 11 2014
prev sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12346



--- Comment #5 from Andrej Mitrovic <andrej.mitrovich gmail.com> 2014-03-12
00:50:09 PDT ---
(In reply to comment #4)
 How ironic!
 http://d.puremagic.com/issues/show_bug.cgi?id=4595

That's a proposed enhancement of the compiler. -- Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Mar 12 2014