Suggestion: properties should be treated as 'virtual members variables'

Kristian kjkilpi at gmail.com
Tue Sep 5 09:40:41 PDT 2006


On Tue, 05 Sep 2006 17:10:59 +0300, mike <vertex at gmx.at> wrote:
> Hi!
>
> I hope you don't mind if I throw in some ideas on that.
>
>> 2. Syntax looks like this:
>>
>> class C
>> {
>> 	private int m_val;
>> 	// Looks just like C# but has the property keyword to distuinguish  
>> from a nested function. This is the simplest type of property.
>> 	property int Val
>> 	{
>> 		get { return m_val; }
>> 		set { m_val = value; }
>> 	}
>> }
>
> One could get even further and expand the C#-style syntax:
>
> (1) Get rid of superflous {} characters when there's only one statement.
>
> ' property int Val
> ' {
> '      get return m_val;
> '      set
> '      {
> '          assert(value < 20);
> '          m_val = value;
> '      }
> ' }
>
> (2) Introduce new "operators". Maybe use operator notation?
>
> ' property int Val
> ' {
> '      get return m_val;
> '      set
> '      {
> '          assert(value < 20);
> '          m_val = value;
> '      }
> '      increase
> '      {
> '          assert(value < 19);
> '          return m_val++;
> '      }
> '      decrease
> '      {
> '          assert(value > 0);
> '          return m_val++;
> '      }
> ' }
>
> (3) Finally - maybe use opXX() notation so there won't be lots of new  
> keywords and with the () it fits more nicely into the syntax.
>
> ' property int Val
> ' {
> '      opReturn() return m_val;
> '      opAssign(int value)
> '      {
> '          assert(value < 20);
> '          m_val = value;
> '      }
> '      opIncrement()
> '      {
> '          assert(value < 19);
> '          return m_val++;
> '      }
> '      opDecrement()
> '      {
> '          assert(value > 0);
> '          return m_val++;
> '      }
> ' }
>
> I like (3) very much.
>
> -Mike


The syntax of (3) is nice if properties can have multiple operators.

(There is very close relation between a property and a class. A property  
has only operators and it's logically treated as a data member of its  
type.)

But, because you cannot overload operators of basic types, how should the  
following work:

void f(inout int v)
{
     v++;
}

f(obj.Val);

The 'opIncrement' operator (or 'opAdd()') of 'Val' cannot be called inside  
'f()'. The answer to this is that the compiler does the following (as was  
described in an earlier message):

auto x = obj.Val; f(x); obj.Val = x;

This means that everything should be doable with the read and write  
operators only. Hence I think that there would not be need for the other  
operators. So (1) looks very nice.



More information about the Digitalmars-d mailing list