digitalmars.D - PropertyType
- Steven Schveighoffer (23/23) May 17 2016 I have this little helper in my iopipe library:
- Edwin van Leeuwen (14/22) May 17 2016 FYI: In painlesstraits we use a different approach, where we test
- Steven Schveighoffer (4/26) May 17 2016 My code is less concerned as to whether it is a property or not. I just
- Timon Gehr (4/6) May 17 2016 typeof((()=>x.foo)())
- Steven Schveighoffer (6/13) May 17 2016 Well, that is likely a valid solution for implementation of
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
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
On 5/17/16 10:50 AM, Edwin van Leeuwen wrote:On Tuesday, 17 May 2016 at 14:24:09 UTC, Steven Schveighoffer wrote:My code is less concerned as to whether it is a property or not. I just want to know the type of the expression :) -SteveI 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
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
On 5/17/16 1:36 PM, Timon Gehr wrote:On 17.05.2016 16:24, Steven Schveighoffer wrote: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 :)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.)Yes, absolutely. I'm not sure if that's fixable at this point, however. -Steve
May 17 2016