digitalmars.D.learn - new properties for basic types
- Dominikus Dittes Scherkl (3/3) Jul 14 2014 Is it possible to write custom properties for basic types, so
- Puming (14/17) Jul 14 2014 yes, just define a funciton with the first parameter int:
- Philippe Sigaud via Digitalmars-d-learn (3/3) Jul 14 2014 Halas, that's not what the OP wants. He needs properties on the *type*
- Dominikus Dittes Scherkl (17/21) Jul 14 2014 Yes, exactly.
- Philippe Sigaud via Digitalmars-d-learn (7/20) Jul 14 2014 @property allows you to call a function without the parenthesis (), to
- Dominikus Dittes Scherkl (7/14) Jul 15 2014 Ah, ok. That means without @property I would need to write
- "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> (10/13) Jul 15 2014 That was the original idea, but today the situation is that for
Is it possible to write custom properties for basic types, so that I can write e.g. "int.myProp" instead of "myProp!int()" [analogue to x.myProp instead of myProp(x)]?
Jul 14 2014
On Monday, 14 July 2014 at 10:28:30 UTC, Dominikus Dittes Scherkl wrote:Is it possible to write custom properties for basic types, so that I can write e.g. "int.myProp" instead of "myProp!int()" [analogue to x.myProp instead of myProp(x)]?yes, just define a funciton with the first parameter int: ```d property int triple(int x) { return x * 3; } void main() { int x = 4; assert(12 == x.triple); } ```
Jul 14 2014
Halas, that's not what the OP wants. He needs properties on the *type* itself: int.foo instead of foo!int. So no, this is not possible.
Jul 14 2014
On Monday, 14 July 2014 at 11:28:15 UTC, Philippe Sigaud via Digitalmars-d-learn wrote:Halas, that's not what the OP wants. He needs properties on the *type* itself: int.foo instead of foo!int.Yes, exactly.So no, this is not possible.Hmm. So how do I use stuff like this: template defaultInit(T) { static if (!is(typeof({ T v = void; }))) // inout(U) property T defaultInit(T v = T.init); else property T defaultInit(); } (this is from std.traits - ok, it's private, but anyway) Because I have seen nowhere anything like defaultInit!T (or T.defaultInit) and don't understand why here the attribute property is used. Why does it make a difference, and how?
Jul 14 2014
Hmm. So how do I use stuff like this: template defaultInit(T) { static if (!is(typeof({ T v = void; }))) // inout(U) property T defaultInit(T v = T.init); else property T defaultInit(); } (this is from std.traits - ok, it's private, but anyway)It should be invoked as `defaultInit!SomeType`Because I have seen nowhere anything like defaultInit!T (or T.defaultInit) and don't understand why here the attribute property is used. Why does it make a difference, and how?property allows you to call a function without the parenthesis (), to imitate a field in a struct or class. In this particular case, I don't know what defaultInit is used for. It seems to compile to a forward declaration of a function, but I don't know what for. I cannot find it on my copy of std.traits. What DMD version are you using?
Jul 14 2014
On Tuesday, 15 July 2014 at 05:26:57 UTC, Philippe Sigaud via Digitalmars-d-learn wrote:property allows you to call a function without the parenthesis (), to imitate a field in a struct or class.Ah, ok. That means without property I would need to write defaultInit!T() instead of defaultInit!T Hmm. I'm not sure that I like this syntax that hides a function call.In this particular case, I don't know what defaultInit is used for. It seems to compile to a forward declaration of a function, but I don't know what for. I cannot find it on my copy of std.traits. What DMD version are you using?Hmm. Ok it was in 2.064. I should update my setup it seems :-)
Jul 15 2014
On Tuesday, 15 July 2014 at 05:26:57 UTC, Philippe Sigaud via Digitalmars-d-learn wrote:property allows you to call a function without the parenthesis (), to imitate a field in a struct or class.That was the original idea, but today the situation is that for all argument-less method calls (and UFCS calls) the parentheses are optional, but for functions with ` property` parentheses _mustn't_ be used. This was mainly done to avoid an ambiguity when the function returns something that is itself callable, but also is useful to keep APIs stable: If you mark a method as ` property`, you can replace it by a member variable and existing code will not break (but needs to be recompiled, of course).
Jul 15 2014