Detecting a property setter?
Simen kjaeraas
simen.kjaras at gmail.com
Mon Jul 19 13:06:14 PDT 2010
Rory McGuire <rmcguire at neonova.co.za> wrote:
> Does anyone know how to detect if there is a setter for a property? The
> code below prints the same thing for both
> the setter and the getter of "front":
>
> ==============================
> import std.traits;
>
> class Foo {
> uint num;
>
> @property ref uint front() {
> return num;
> }
> @property void front(uint i) {
> num = i;
> }
>
> ref uint front2() {
> return num;
> }
> }
>
> template isProperty(alias func) if (isCallable!(func)) {
> enum isProperty = (functionAttributes!(func) &
> FunctionAttribute.PROPERTY)==0 ? false : true;
> }
>
> template hasSetter(alias func) if (isCallable!(func)) {
> enum hasSetter = (isProperty!(func) && ParameterTypeTuple!(func).length
> >
> 0 && ReturnType!(func).stringof != "void") ? true : false;
> }
For what it's worth, I would simply check if the property allows
assignment. i.e.:
template hasSetter(alias func) if (isCallable!(func)) {
enum hasSetter = isProperty!(func) &&
is( typeof( (){ func = ReturnType!(func).init; } ) );
}
--
Simen
More information about the Digitalmars-d-learn
mailing list