@property needed or not needed?

Zach the Mystic reachBUTMINUSTHISzach at gOOGLYmail.com
Mon Jan 28 10:08:20 PST 2013


On Monday, 28 January 2013 at 03:24:09 UTC, Steven Schveighoffer 
wrote:
> There are three intentions when creating a function w/ regards 
> to properties:
>
> 1. You intend the function to be called without parentheses to 
> clarify it is a property.
> 2. You intend the function to be only called with parentheses 
> to clarify it is a function.
> 3. You don't care whether it's called with parentheses or not, 
> because the name of the function is clearly a property or a 
> function.

This is a good point. Possibly since I'm the author of the 
following proposal, but also because I hope it will garner 
greater consideration than it has so far, this is how you would 
do these with my suggested language features single-instance 
structs and opGet:

The new syntax changes:

struct __foo {}
__foo foo;

to:

foo struct {}

This makes single-instance structs as easy to write as lambda 
functions. Combining them with two new operator definitions opGet 
and... let me see... opDo should cover it... it would look like 
this:

struct Goo
{
   int _n;
   foo struct
   {
     int opGet() { return _n; } // Exactly like opCall, but may 
not be used with parentheses
   }
}

Goo g;
g.foo(); // Error

A struct may have exactly one of opGet, opCall, and opDo.

foo struct
{
   int opGet() { ... }
   int opDo() { ... } // Error: a struct may not have both opGet 
and opDo
}

opDo does exactly the opposite of opGet: it mandates the uses of 
parentheses instead of prohibits them. opCall, on the other hand, 
permits either one freely.

foo struct { int opDo() { return 4; } }
foo; // Error: foo must be called with parentheses

Note how the single-instance struct does not store data of its 
own, but is used for its value as a namespace and its ability to 
overload variables. That doesn't mean you *can't* store data in 
it. It's just like any other struct except that is has what I was 
thinking of calling a Highlander type: There can be only one!


More information about the Digitalmars-d mailing list