Property Discussion Wrap-up

From D Wiki
Revision as of 21:14, 25 January 2013 by Dicebot (talk | contribs) (Undo revision 1712 by Dicebot (talk))
Jump to: navigation, search

This is a wrap-up of the property discussion which happened around 25. January 2013. There have been some discussion before and there might be more in the future :-)

Except from off-topic, meta and personal discussions which are not summarized on this page these two issues were discussed:

Link to discussion

If you've got too much time, the original discussions can be read here:

DIPS:

Current

Superceded

Property

  • Everyone agrees that @property doesn't work well right now. People disagree what's the actual problem with @property. Some people where arguing to remove @property completely. It's not clear if those where advertising making normal functions callable like @properties or removing @property without replacement.

Why @property

As there were suggestions removing @property completely we should think about what problem @properties should solve:

  • Properties are a replacement for fields
    • Unlike fields, they are polymorphic
    • As they are supposed to be used as fields, they are for data access and nothing else
    • They are especially not there to make function calls without parentheses. This is a different issue. To the developer properties are not functions, they're enhanced fields. The fact that they are implemented as functions is a implementation detail.
      • D as a systems programming language should still provide a way to detect if something is a property and to get the getter/setter. This shouldn't be visible in normal usage.
    • Replacing a field by a property should be transparent
  • Properties allow executing additional code. Checking the supplied value, firing events,...
    • As a property looks like data access, complexity should be kept constant.
  • Properties allow making a field readonly (public get, private/no set), readwrite(public get/set) or write-only (private/no get, public set).


An example for property usage:

struct A
{
    int _days;
    @property int weeks()
    {
        return _days/7;
    }

    @property void weeks(int value)
    {
        assert(value <= 42);
        _days = value*7;
    }
}

void use()
{
    A a;
    a.weeks = 3;
    a.weeks++;
    a.weeks -=2;
    typeof(a.weeks) b = a.weeks;
}


Now consider how to implement the above if properties where completely removed, as proposed. The D1 solution allowed assignment syntax for normal functions. So the above example could work exactly the same way, but @property wouldn't be used. This is a poor solution for properties as it allows to write nonsensical code:

void use()
{
    writeln = 42; //would call writeln(42);
    writeln += 1; //How would this even be implemented without @property?
}

Note that this is in no way related to the optional parentheses discussion: You can have parentheses, a @property implementation and still disallow code like writeln = 42;

Property declaration syntax

Some new syntaxes have bee proposed for property declaration:

  • Could make declaring properties less verbose
  • Would break lots of code
    • Could be done without breakage with new attributes (@prop), but that's ugly
  • There's no need for a new syntax. Although the current syntax is verbose, it does not have real issues. The issues happen when using properties, not when declaring them.
    • The allowed function prototypes (ref) should be reviewed though and it should be made clear how they interact:


@property ref int prop();
@property int prop();
@property void prop(int value);
@property int prop(int value);
@property void prop(ref int value); //not as problematic

Implementation concerns

  • Can we get a reference to the property? What does &x.property_ mean?
  • How can we get the getter / setter functions? Do we need to get those?
  • What is the type of the property? Return type, setter function type or getter function type? How to get the other types?
  • What does x.property_++ do? (Semantic rewriting)
  • Is returning ref values from the getter OK?
  • Is taking ref values in the setter OK?
  • Should all properties be nothrow? pure? getter const?
    • Probably better as a rule for style guide.
  • Are UFCS properties possible? How do they work exactly?
  • How do you disambiguate property functions when they're free functions which conflict?
    • Normal UFCS functions can be force called by using their fully qualified name. That's not possible for properties if function call syntax is disallowed?

Proposals

Proposal 1

  1. @property functions can't be called with parentheses: x._property() is illegal! (Normal functions are explicitly not affected by this rule!)
    1. The only exception to this rule: If the returned value can be called (delegate, function pointer, opCall), then x._property() calls the returned value! It essentially behaves exactly as a field would.
  2. Getting a reference is illegal: &x._property does not compile.
    1. Rationale: Returning a reference to a temporary is not the same as taking the address of a field and is therefore confusing and dangerous in generic code. A real pointer could be returned if the property getter returned a value by ref. But then it would only work for some properties, could potentially bypass setters, would break if a setter was added later...
  3. To detect if something is a property or a field, use __traits(isProperty, x._property)! Do not use any tricks in user code to detect if something is a property or field.
  4. The getter/setter function can be obtained via __trais: __traits(propertyGetter, x._property) __traits(propertySetter, x._property)
    1. The function types can be obtained like this: typeof(__traits(propertyGetter, x._property)) == int delegate()
  5. The type of the returned value is obtained with typeof: typeof(x._property) == int
  6. The compiler uses semantic rewriting to make @property access as similar as field access as possible.
  7. Property functions cannot return ref values (as this could bypass the setter)
  8. The setter may take it's parameter by ref.
  9. The -property compiler switch is removed
  10. @property on free functions
  11. A setters parameter type and the corresponding getters return type must match
    1. Two categories: UFCS properties and global properties (see example)
struct Type
{
    @property void native(int);
}

//Global properties:
//returns void, has 1 parameter --> global setter
@property void external1(int);
//returns !void, has 0 parameters --> global getter
@property int external1();

//Used like a global variable:
void a()
{
    external1 = 41;
    // 41.external1; is
    // invalid, a getter can't return void (void + 1 args is always a global variable property)
    external1++;
    assert(external1 == 42);
}

//UFCS properties:
//returns void, has 2 parameters --> global setter
@property void external2(Type, int);
//returns !void, has 1 parameters --> global getter
@property int external2(Type);

//external2 can only be called on a Type instance.
//calling it manually "external2(Type(), 42)" is invalid

//Used like a property/field declared on Type:
void a()
{
    Type t;
    t.external2 = 41;
    t.external2++;
    assert(t.external2 == 42);
    //all "t.external1" combinations are invalid! 
}


Unified test suite
alias Deleg = int delegate();

struct Test
{
    int var;
    
    @property void native2(int param = 1337) { } //invalid. default param creates invalid case with no args which returns void
    @property void native(int) { } //OK
    @property int native() { return 42; } //OK
    
    @property void strange() { } //not allowed
    
    @property Deleg delegRet() { return () => 42; } //OK
}
 
@property void external1(int) { } //OK (Global setter)
@property int external1() { return 42; } //OK (Global getter)

@property void external2(Test, int) { } //OK (UFCS setter)
@property int external2(Test) { return 42; } //OK (UFCS getter)
 
void main()
{
    Test t;
    t.native = 42; //OK
    t.native(42); //invalid
    int a = t.native; //OK
    t.strange; //invalid (definition of strange already invalid)
    
    external1 = 42; //OK
    external1(42); //invalid
    int b = external1; //OK    
    42.external1; //invalid
    
    t.external2 = 42; //OK
    auto c = t.external2; //OK
    external2(t).external1; //invalid
    
    t.delegRet; //basically valid, could complain about statement has no effect if delegRet getter is pure
    t.delegRet(); //valid, gets delegate then calls it(but as the result
                  //of the delegate call isn't used the compiler could complain if the delegate is pure)
    t.delegRet()(); //invalid
    
    pragma(msg, is(typeof(t.var) == typeof(t.native))); //OK
    
    pragma(msg, typeof(external1)); //int
    import std.traits;
    // pragma(msg, typeof(ReturnType!(typeof(external1)))); // error (typeof(external1) returns int, not a function type)
    
    pragma(msg, typeof(&(t.delegRet))); //illegal, can't get address of temporary? (Not sure here)
    pragma(msg, typeof(t.delegRet));    //int delegate()
    pragma(msg, typeof(t.delegRet()));  //OK (is this valid D code in general?)
    pragma(msg, typeof(t.delegRet()())); //invalid
    
    pragma(msg, ReturnType!(typeof(t.delegRet))); //int
}

Proposal 1.1

  1. Like proposal 1, but instead of rule 7, returning ref values is allowed as long as there is not setter function.

Proposal 1.2

This is similar to 1 and 1.1, but draws a clear distinction between @property functions and non-property functions.

  1. Basically, marking a function or method as @property turns it into a variable-like entity, such that to the outside world, it behaves exactly like a variable, and not like a function.
    1. If a prop() is a @property method in some struct or class X, then it behaves as if you declared a variable of its return type as a member variable in X.
    2. Member functions can be declared @property only if they take zero arguments (getter) or exactly one argument (setter).
    3. Writing "prop()" invokes opCall of its return value. If the return type does not support opCall, this is a compile error.
    4. Writing "prop" returns a value of prop's return type (i.e., it invokes the function to compute the value).
    5. Writing "&prop" is legal only if (1) func returns by ref, and (2) there is no setter method prop(T). It simply returns the address of the returned value. It does not return a function pointer to prop. (To the outside world, prop is a variable, not a function.)
    6. It is legal to write prop = x; this calls the setter for prop.
    7. All assignment syntax for variables should work transparently for @property's: prop++ should be lowered to prop(prop()+1), etc..
  2. UFCS & module-level functions:
    1. It is legal to declare a module-level functions as @property if they take 0, 1 or 2 arguments:
      1. @property T globalVar() {...} is a getter, behaves like declaring "T globalVar".
      2. @property void globalVar(T) {...} is a setter, you access it by writing "globalVar = t"
      3. @property T ufcsProp(U) is a getter, behaves as if you declared a member variable "T ufcsProp" in the type U.
      4. @property void ufcsProp(U, T) {...} is a setter, you access it by writing "u.ufcsProp = t".
    2. All other cases are illegal.
  3. @property functions are NOT normal functions:
    1. It is illegal to use assignment syntax for non-@property functions. If there is a function f(int), it is illegal to write "f = 123". Assignment syntax is reserved for variables only (and by extension, @property functions, because @property means they appear like variables to the outside world).
    2. It is illegal to call a @property function using regular function-call syntax: if you have @property void ufcsProp(U, T) {...}, then it is illegal to write "ufcsProp(u,t)". Instead, you must write "u.ufcsProp(t)".
  4. Introspection:
    1. Basically, @property functions are indistinguishible from variables.
    2. For code that needs to tell the difference, __traits(...) should be extended to detect them. Perhaps something like:
      1. __traits(isPropertyFunction, x) -- returns true if it's a @property function
      2. __traits(propertyFunctionType, x) -- returns function signature of the @property function, error if x is not a @property function


  • I think most of this could me merged to 1. On a quick look the only difference seems to be this proposal has less traits (can't access getter/setter function) and this allows &prop. This proposal also makes it clear that properties are treated like/variables fields. The first proposal doesn't state it as clearly, but this is also meant in #1. How about merging the generic part into #1 and only listing those differences here?

Corner cases to consider

@property on free functions

struct Type
{
    @property void native(int);
}

@property void external1(int);       // valid? (no assignment context)
@property void external2(Type, int); // valid? (extra parameter comparing to typical setter)

void main()
{
    Type t;
    t.native = 42;    // typical
    external1 = 42;   // allowed or not?
    42.external1;     // allowed or not?
    t.external2 = 42; // allowed or not?
}

opDispatch and properties

If you make opDispatch @property, then you can't use it with normal functions, and if you don't make it @property, then it can't be used with property functions. And you can't overload on @property, so opDispatch is pretty much screwed with regards to properties at this point.

Optional parentheses

Pro

  • UFCS code looks nicer:
auto yesterday = 2.days.ago;

some!(e1).ufcs!(e2).chaining!(e3)
//instead of
some!(e1)().ufcs!(e2)().chaining!(e3)()

//We need a real world UFCS case demonstrating this.
//There are real world examples - even in that discussion thread - but I cant find them ;-)

Cons

  • Ambiguous / complicated if a function returns a delegate or similar (solvable with special case rules)
  • Complicates semantics for human reader of the code (see comments about readability in "How those are (not!) related")

How those are (not!) related to properties

Both properties and optional parentheses are basically orthogonal. It's possible to have both, have none and have any mix of those.

The small issue where they interfere is code readability:

If optional parentheses are disallowed, it's easy to see that

auto var = new someClass();
auto x = var.something;

will always be either a field access or a property call. But according to property definition (as used by C# and other languages), properties should behave like fields (i.e. constant complexity, no side-effects,...). Therefore without optional parentheses it's possible to see if an access is actually a potentially heavy weight function call or data / property access.

Except from this, @property and optional parentheses are orthogonal.

Unified test suite

Aims to provide one source file that provides all use cases that work with current lax rules. Any proposal should give clear answer, which of them are allowed and how those should work.

alias Deleg = int delegate();

struct Test
{
	int var;
	
	@property void native(int param = 1337) { }
	@property int native() { return 42; }
        
        @property int both1(int a) { return a; }

        @property ref int both2() { return var; }
	
	@property void strange() { }
	
	@property Deleg delegRet() { return () => 42; }
}
 
@property void external1(int) { } 
@property int external1() { return 42; }

@property void external2(Test, int) { }
@property int external2(Test) { return 42; }

@property void lotof(Test, int, int, int) { }
 
void main()
{
    Test t;
    t.native = 42;
    t.native;
    t.native(42);
    int a = t.native;
    t.native++;
    t.native += 42;
    t.strange;
    t.both1 = 42;
    a = t.both1;
    t.both2 = 42;
    a = t.both2;
	
    external1 = 42;
    external1(42);
    int b = external1;	
    42.external1;
	
    t.external2 = 42;
    auto c = t.external2;
    external2(t).external1;
	
    t.delegRet;
    t.delegRet();
    t.delegRet()();
	
    pragma(msg, is(typeof(t.var) == typeof(t.native)));
	
    pragma(msg, typeof(external1));
    import std.traits;
    // pragma(msg, typeof(ReturnType!(typeof(external1)))); // Error now
	
    pragma(msg, typeof(&(t.delegRet)));
    pragma(msg, typeof(t.delegRet));
    pragma(msg, typeof(t.delegRet()));
    pragma(msg, typeof(t.delegRet()()));
	
    pragma(msg, ReturnType!(typeof(t.delegRet)));
}