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



/********** 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



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




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



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




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




00:50:09 PDT ---

 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