What is the current stage of @property ?

H. S. Teoh hsteoh at quickfur.ath.cx
Thu Jun 11 05:41:25 UTC 2020


On Wed, Jun 10, 2020 at 10:58:57PM -0600, Jonathan M Davis via Digitalmars-d-learn wrote:
[...]
> As things stand, @property has no real practical purpose but
> frequently gets used to indicate that it's the intention of a
> function's author for it to be used as if it were a variable. I
> suspect that it's also frequently misunderstand that it's required if
> you want to call a function without parens. So, you're likely to see
> @property in quite a lot of D code, but ultimately, all it's really
> doing is serving as documentation of the author's intent and screwing
> up metaprogramming.
[...]

Ironically, just today I ran into this corner case where @property
actually became a solution to a real problem:

	//-----module1.d------
	auto someGenericFunc(T)(T t) {
		...
		static if (is(typeof(T.init.method) : T)) {
			R someRange = ...; // ElementType!R == T
			auto value = someRange.map!(e => e.method);
		}
		...
	}

	//-----module2.d------
	struct MyType {
		...
		auto method()() {
			return ...;
		}
		...
	}

	//------module3.d-----
	import module1;
	import module2;
	auto someFunc(...) {
		auto result = someGenericFunc!MyType;
	}

This was failing compilation, because when MyType.method is a template,
the static if condition in module1 fails.  Adding @property to
MyType.method tells the compiler that `T.init.method` is intended to
refer to the return value rather than the template function itself, and
thus neatly solves the problem.


T

-- 
It's amazing how careful choice of punctuation can leave you hanging:


More information about the Digitalmars-d-learn mailing list