[Issue 8726] New: About immutable and const constructors
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Tue Sep 25 23:31:08 PDT 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8726
Summary: About immutable and const constructors
Product: D
Version: D2
Platform: x86
OS/Version: Linux
Status: NEW
Severity: normal
Priority: P2
Component: DMD
AssignedTo: nobody at puremagic.com
ReportedBy: freeslave93 at gmail.com
--- Comment #0 from Roman <freeslave93 at gmail.com> 2012-09-25 23:31:52 PDT ---
Hello to all. Actually I posted this issue on D forums, but I was advised to
ask there. I want to discuss immutable and const constructors.
For a start let's write the simple class with two constructors:
class A
{
int x;
this() immutable
{
x = 42;
}
this()
{
x = 13;
}
}
void main()
{
//
}
Well, dmd passes this code, but what if we want to create some
object of class A?
void main()
{
A a = new A;
}
hello.d|177|Error: constructor hello.A.this called with argument
types:|
hello.d|177|Error: cannot implicitly convert expression (new A)
of type immutable(A) to hello.A|
Class A has mutable constructor, but for some reason immutable
one is called and then immutable object can not be converted to
type A, which is mutable.
Let's rewrite this line to this:
immutable A a = new A;
hello.d|173|Error: constructor hello.A.this called with argument
types:|
So what's the problem? Yes, both A's constructors really have
empty argument lists, but... ah. Let's try another way:
immutable A a = new immutable A;
hello.d|173|Error: found 'A' when expecting '('|
hello.d|173|Error: basic type expected, not ;|
hello.d|173|Error: found ';' when expecting ')'|
hello.d|174|Error: semicolon expected, not '}'|
Again not. Looks like it's not correct statement at all. So I
conclude there is no way to keep simultaneously both mutable and
immutable constructors with same argument list. But as we saw
above, compiler passes code if we don't create objects of class
A, although such permission has no sense.
Well, now let's rewrite our class using const specifier instead
of immutable
class A
{
int x;
this() const
{
x = 42;
}
this()
{
x = 13;
}
}
void main()
{
const A a = new A;
assert(a.x == 42);
}
Project is built without errors or warning, but we get assertion
failure in runtime. Error seems obvious: new A is constructed
like mutable and only then assigned to object of class const A.
Maybe the following code will be correct?
const A a = new const A;
hello.d|173|Error: found 'A' when expecting '('|
hello.d|173|Error: basic type expected, not ;|
hello.d|173|Error: found ';' when expecting ')'|
hello.d|174|Error: semicolon expected, not 'assert'|
But we get same errors as with immutable specifier.
I think, there is some compiler defect or problem of language design.
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
More information about the Digitalmars-d-bugs
mailing list