www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Should it be a compile time error?

reply "deed" <none none.none> writes:
The following compiles and crashes with DMD 2.063.
Should this be a compile time error?


class A
{
     int _var;

     void var(int i)  property
     {
         this.var = i;   // oops, crashes.
     }                   // should have been this._var

     int var()  property
     {
         return var;
     }
}

void main()
{
     auto a = new A();
     a.var = 3;
}
Jun 19 2013
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
deed:

 Should this be a compile time error?
In general it's not easy for a D compiler to perform that kind of validation. Bye, bearophile
Jun 19 2013
prev sibling next sibling parent reply "Iain Buclaw" <ibuclaw ubuntu.com> writes:
On Wednesday, 19 June 2013 at 11:33:43 UTC, deed wrote:
 The following compiles and crashes with DMD 2.063.
 Should this be a compile time error?


 class A
 {
     int _var;
/* SNIP */
     int var()  property
     {
         return var;
     }
Isn't the problem in this property function? (Shouldn't it return _var :o)
Jun 19 2013
next sibling parent "deed" <none none.none> writes:
 /* SNIP */

    int var()  property
    {
        return var;
    }
Isn't the problem in this property function? (Shouldn't it return _var :o)
that's also an error. changing to _var gives same result though..
Jun 19 2013
prev sibling parent reply dennis luehring <dl.soluz gmx.net> writes:
Am 19.06.2013 13:40, schrieb Iain Buclaw:
 On Wednesday, 19 June 2013 at 11:33:43 UTC, deed wrote:
 The following compiles and crashes with DMD 2.063.
 Should this be a compile time error?


 class A
 {
     int _var;
/* SNIP */
     int var()  property
     {
         return var;
     }
Isn't the problem in this property function? (Shouldn't it return _var :o)
that isn't the problem - D allows assignment to an read property - and there is no write property around, so it should be an compiletime error i should compile only if the missing write property is available - or? property int var(int value) { return _var = value; }
Jun 19 2013
parent dennis luehring <dl.soluz gmx.net> writes:
 that isn't the problem - D allows assignment to an read property - and
 there is no write property around, so it should be an compiletime error

 i should compile only if the missing write property is available - or?

  property int var(int value) { return _var = value; }
sorry i've totaly lost seeing the write property :(
Jun 19 2013
prev sibling parent "John Colvin" <john.loughran.colvin gmail.com> writes:
On Wednesday, 19 June 2013 at 11:33:43 UTC, deed wrote:
 Should this be a compile time error?
Yes it should, in an ideal world. Expanding out the assign property, what we've got is: void var(int i) { var(i); // endless recursion. } Linux segfaults on stack overflow, so that's your crash.
Jun 19 2013