www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - PropertyType

reply Steven Schveighoffer <schveiguy yahoo.com> writes:
I have this little helper in my iopipe library:

template PropertyType(alias x)
{
     static if(is(typeof(x) == function))
         alias PropertyType = typeof(x());
     else
         alias PropertyType = typeof(x);
}

This is because when you have code like this:

x.foo

And you are introspecting some type, you can't tell whether this is a 
property function or a field.

If foo is a field, or a function marked as a  property, then 
typeof(x.foo) will be whatever x.foo returns. But if foo is a straight 
function, then typeof(x.foo) will be a function type.

As we all know,  property is not exactly popular, and having to mark 
functions  property just for introspection purposes (applying the tag 
actually has zero impact otherwise) seems incorrect.

But of course, if I want to know what type the expression x.foo will 
yield, I have to do the above.

Is there a use for this in Phobos? Is it already present somewhere? 
Where should it go if it's not there already?

-Steve
May 17 2016
next sibling parent reply Edwin van Leeuwen <edder tkwsping.nl> writes:
On Tuesday, 17 May 2016 at 14:24:09 UTC, Steven Schveighoffer 
wrote:
 I have this little helper in my iopipe library:

 template PropertyType(alias x)
 {
     static if(is(typeof(x) == function))
         alias PropertyType = typeof(x());
     else
         alias PropertyType = typeof(x);
 }
FYI: In painlesstraits we use a different approach, where we test whether it is a function (with isSomeFunction) and then test for the property attribute: https://github.com/msoucy/painlesstraits/blob/master/source/painlesstraits.d#L173 ``` static if (isSomeFunction!(T)) { return (functionAttributes!(T) & FunctionAttribute.property); } else return false; ```
May 17 2016
parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 5/17/16 10:50 AM, Edwin van Leeuwen wrote:
 On Tuesday, 17 May 2016 at 14:24:09 UTC, Steven Schveighoffer wrote:
 I have this little helper in my iopipe library:

 template PropertyType(alias x)
 {
     static if(is(typeof(x) == function))
         alias PropertyType = typeof(x());
     else
         alias PropertyType = typeof(x);
 }
FYI: In painlesstraits we use a different approach, where we test whether it is a function (with isSomeFunction) and then test for the property attribute: https://github.com/msoucy/painlesstraits/blob/master/source/painlesstraits.d#L173 ``` static if (isSomeFunction!(T)) { return (functionAttributes!(T) & FunctionAttribute.property); } else return false; ```
My code is less concerned as to whether it is a property or not. I just want to know the type of the expression :) -Steve
May 17 2016
prev sibling parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 17.05.2016 16:24, Steven Schveighoffer wrote:
 But of course, if I want to know what type the expression x.foo will
 yield, I have to do the above.
typeof((()=>x.foo)()) (Arguably, function types should not even exist and typeof(x.foo) should give you what you want.)
May 17 2016
parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 5/17/16 1:36 PM, Timon Gehr wrote:
 On 17.05.2016 16:24, Steven Schveighoffer wrote:
 But of course, if I want to know what type the expression x.foo will
 yield, I have to do the above.
typeof((()=>x.foo)())
Well, that is likely a valid solution for implementation of PropertyType, but I'd never want to have to type that wherever I wanted the property return type :)
 (Arguably, function types should not even exist and typeof(x.foo) should
 give you what you want.)
Yes, absolutely. I'm not sure if that's fixable at this point, however. -Steve
May 17 2016