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

H. S. Teoh hsteoh at quickfur.ath.cx
Sat Jan 26 22:40:40 PST 2013


On Sun, Jan 27, 2013 at 03:54:54AM +0100, Adam D. Ruppe wrote:
> It would be cool if we could declare variables with anonymous types;
> 
> struct { opAssign() {} } foo;

I was thinking we could just use a template to alleviate much of the
boilerplate of creating a property using a struct. For example,
something like:

	struct Property(T, alias setter, alias getter) {
		@property T get() {
			return nullaryFunc!setter();
		}
		alias get this;

		void opAssign(T val) {
			return unaryFunc!setter(val);
		}

		// ... add other standard stuff like opAssignOp, etc.
	}

Then you could use it like this:

	struct S {
		int x, y;
		Property!(int, ()=>x+y, (int z) { y=z-x; }) derivedProp;
	}

The syntax could do with some improvement, though. Maybe an alias:

	struct S {
		alias Property!(int, ()=>x+y, (int z) { y=z-x; }) DerivedProp;

		int x, y;
		DerivedProp derivedProp;
	}
	S s;
	s.derivedProp = 123;
	auto ptr = &s.derivedProp;
	writeln(*ptr);
	// etc.


T

-- 
Arise, you prisoners of Windows / Arise, you slaves of Redmond, Wash, /
The day and hour soon are coming / When all the IT folks say "Gosh!" /
It isn't from a clever lawsuit / That Windowsland will finally fall, /
But thousands writing open source code / Like mice who nibble through a
wall. -- The Linux-nationale by Greg Baker


More information about the Digitalmars-d mailing list