Properties

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sat Jan 10 20:50:03 PST 2009


Miles wrote:
> Daniel Keep wrote:
>> Yes, property syntax can simplify some cases, but this isn't one of them.
> 
> One good distinction properties and normal functions ought to make is
> that functions should never be called without (), and properties should
> never have () unless it has a function type.
> 
> Current syntax allows crazy things like:
> 
> ----------
> 	exit = 1;	// it is not an assignment
> 	x = toString = getenv = "PATH";	// creepy, but valid D
> 
> 	if (fork == 1)	// not comparing the value of a variable
> ----------

Walter and I see eye to eye that a possible solution would be to only 
allow the a = b syntax as an alternative for a(b) only if there's also a 
function a(). All of the above can be fixed within that framework.

> Also, currently, in D, if you have a property of a delegate type, you
> will have a mess.
> 
> ----------
> 	int func2() { return 42; }
> 
> 	/* this is a property */
> 	int function() prop() {
> 		return &func2;
> 	}
> 	
> 	void main() {
> 		auto x = prop;		// ok, x is a func ptr
> 		auto y = prop();	// unexpected: y is not int :-(
> 		static assert (is(typeof(y) == int));
> 	}
> ----------
> 
> With properties, you forbid the above syntaxes.

Well the above syntaxes could be forbidden other ways too.


Andrei



More information about the Digitalmars-d mailing list