@property - take it behind the woodshed and shoot it?

Adam Wilson flyboynw at gmail.com
Sat Jan 26 11:21:47 PST 2013


On Sat, 26 Jan 2013 11:10:20 -0800, Maxim Fomin <maxim at maxim-fomin.ru>  
wrote:

> On Saturday, 26 January 2013 at 13:21:37 UTC, Jacob Carlborg wrote:
>> It's always possible to avoid keywords in favor of syntax. Example:
>>
>> Declaring a getter:
>>
>> int foo {}
>>
>> Just as a regular function declaration but without the parentheses.
>>
>> Declaring a setter:
>>
>> void foo= (int value) {}
>>
>> Append an equal sign to the function name.
>
> This looks nice, but I favor for C# properties.
>
> The root of the issue is that in C/C++/D there is tremendous difference  
> between object types and functions types (which are incompatible) and  
> property is like a bridge between them - I think that is why the feature  
> is demanded.
>
> However in D a property is essentially a function. Few characteristics  
> that are intrinsic to data types are typeof(prop) which is data type for  
> properties and parenthesis-less access. There is no property as a  
> special entity per se.
>
> In C# property and getter/setter are separated, so there is no confusion  
> between data and functions. In D it would look like this:
>
> class A
> {
>      private int i;
>      @property int foo // may be without @property at all?
>      {
>          get { return i; }
>          set { i = @value; }
>      }
> }
>
> In this solution property is not defined by naming of two separate  
> functions and is independent of any function in general.

C# does not have a property keyword precisely because get/set are enough  
for the compiler to determine whether or not it is a property.

So this is completely valid C#:

class A
{
      private int i;
      public int foo
      {
          get { return i; }
          set { i = @value; }
      }
  }

-- 
Adam Wilson
IRC: LightBender
Project Coordinator
The Horizon Project
http://www.thehorizonproject.org/


More information about the Digitalmars-d mailing list