www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - const/invariant bug?

reply "Craig Black" <craigblack2 cox.net> writes:
Unless I misunderstand something this shouldn't compile.  But it compiles 
fine in DMD 2.012.  Is this a bug?

import std.stdio;

class A
{
public:
  int x = 0;
  void setX(int nx) const { x = nx; }
}

void foo(const A a) { a.setX(1); }

int main(char[][] args)
{
  A a = new A;
  foo(a);
  writefln(a.x);
  return 0;
}
Apr 02 2008
next sibling parent Walter Bright <newshound1 digitalmars.com> writes:
You're right, it shouldn't compile.
Apr 02 2008
prev sibling parent reply Walter Bright <newshound1 digitalmars.com> writes:
Craig Black wrote:
  void setX(int nx) const { x = nx; }
That should work, but at the moment: const void setX(int nx) { x = nx; } does work.
Apr 03 2008
parent reply Extrawurst <spam extrawurst.org> writes:
Walter Bright schrieb:
 Craig Black wrote:
  void setX(int nx) const { x = nx; }
That should work, but at the moment: const void setX(int nx) { x = nx; } does work.
Why do we need the suffix "const" anyway ? Isnt unambiguity a virtue ?
Apr 03 2008
parent reply Walter Bright <newshound1 digitalmars.com> writes:
Extrawurst wrote:
 Why do we need the suffix "const" anyway ? Isnt unambiguity a virtue ?
Some people prefer it.
Apr 03 2008
next sibling parent Extrawurst <spam extrawurst.org> writes:
Walter Bright schrieb:
 Extrawurst wrote:
 Why do we need the suffix "const" anyway ? Isnt unambiguity a virtue ?
Some people prefer it.
Some people seem to prefer untransitive const too ;) (i am not one of those, to pe clear on that)
Apr 03 2008
prev sibling next sibling parent Derek Parnell <derek psych.ward> writes:
On Thu, 03 Apr 2008 11:33:10 -0700, Walter Bright wrote:

 Extrawurst wrote:
 Why do we need the suffix "const" anyway ? Isnt unambiguity a virtue ?
Some people prefer it.
Does that principal also apply to 'alias' or 'maifest' or 'define' instead of enum for manifest constants? -- Derek Parnell Melbourne, Australia skype: derek.j.parnell
Apr 03 2008
prev sibling parent "Dave" <Dave_member pathlink.com> writes:
"Walter Bright" <newshound1 digitalmars.com> wrote in message 
news:ft380r$13vn$1 digitalmars.com...
 Extrawurst wrote:
 Why do we need the suffix "const" anyway ? Isnt unambiguity a virtue ?
Some people prefer it.
 const void setX(int nx) { x = nx; }
Since there has, understandably, been some confusion about what const refers to with the prefixed form, why don't we get rid of the prefixed form altogether for 2.0? You could still allow: class C { const { int[] foo() { } // int[] foo() const { } double[] bar() { } // double[] bar() const { } } } because you have to do const(int[]) foo() to specify it for the return type anyhow, which is consistent between member and non-member functions. I doubt at this point it would break all that much code and the deprecated switch could serve in the interim. I really think this change would be worth it considering all of the confusion the prefix form will cause down the road, especially for C++ users. Thanks, - Dave
Apr 05 2008