Properties

Miles _______ at _______.____
Sat Jan 10 18:05:45 PST 2009


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
----------

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.

So, what is "better" about properties is not shorter syntax, but giving
proper semantics for a given symbol.

Also, properties can be part of interfaces, enforcing derived classes to
implement them in a given way.



More information about the Digitalmars-d mailing list