Thread overview
Should it be a compile time error?
Jun 19, 2013
deed
Jun 19, 2013
bearophile
Jun 19, 2013
Iain Buclaw
Jun 19, 2013
deed
Jun 19, 2013
dennis luehring
Jun 19, 2013
dennis luehring
Jun 19, 2013
John Colvin
June 19, 2013
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;
}
June 19, 2013
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
June 19, 2013
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)

June 19, 2013
> /* 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..
June 19, 2013
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; }

June 19, 2013
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.
June 19, 2013
> 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 :(