Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
July 14, 2014 new properties for basic types | ||||
---|---|---|---|---|
| ||||
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)]? |
July 14, 2014 Re: new properties for basic types | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dominikus Dittes Scherkl | 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);
}
```
|
July 14, 2014 Re: new properties for basic types | ||||
---|---|---|---|---|
| ||||
Posted in reply to Puming | 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. |
July 14, 2014 Re: new properties for basic types | ||||
---|---|---|---|---|
| ||||
Posted in reply to Philippe Sigaud | 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? |
July 15, 2014 Re: new properties for basic types | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dominikus Dittes Scherkl | > 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? |
July 15, 2014 Re: new properties for basic types | ||||
---|---|---|---|---|
| ||||
Posted in reply to Philippe Sigaud | 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 :-) |
July 15, 2014 Re: new properties for basic types | ||||
---|---|---|---|---|
| ||||
Posted in reply to Philippe Sigaud | 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).
|
Copyright © 1999-2021 by the D Language Foundation