Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
June 14, 2015 @property on free function for UFCS? | ||||
---|---|---|---|---|
| ||||
Suppose I have a function defined like so: void foo(int i) { } intended to be called like: 5.foo Should it be labeled with @property? Or is @property only for true member functions? |
June 14, 2015 Re: @property on free function for UFCS? | ||||
---|---|---|---|---|
| ||||
Posted in reply to rcorre | You can use @property there, but you don't have to because you can call it with optional parenthesis anyway. |
June 14, 2015 Re: @property on free function for UFCS? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Sunday, 14 June 2015 at 12:36:43 UTC, Adam D. Ruppe wrote:
> You can use @property there, but you don't have to because you can call it with optional parenthesis anyway.
Thanks.
Is there a good reference for the current state of @property?
I know it was hotly debated for awhile (and maybe still is?).
I'm just never sure when I should be using it (if at all).
|
June 14, 2015 Re: @property on free function for UFCS? | ||||
---|---|---|---|---|
| ||||
Posted in reply to rcorre | On Sunday, 14 June 2015 at 12:53:43 UTC, rcorre wrote: > Is there a good reference for the current state of @property? Easy: it does absolutely nothing right now. > I'm just never sure when I should be using it (if at all). You should really only use it when you know the function is semantically the same as a field, aka if you want the @property function to be entirely invisible. Don't ask if you want it callable without parens, likely ALL functions will remain that way, instead ask if you want it to be entirely substitutable for the return value. I'd say in practice, only use it when you're returning a callable and want a.foo() to call the return value (so the fact that foo is actually a function is entirely hidden) or if you're returning a ref and want &a.foo to give the address of the referenced variable instead of a delegate to the function (again, the fact that it is a function is entirely hidden, it acts identically to the variable). Though note that even now, since @property doesn't do anything yet, you'd still have to call it explicitly, which will mean compile errors when it is finally implemented... So perhaps best is to just not use it at all. |
June 14, 2015 Re: @property on free function for UFCS? | ||||
---|---|---|---|---|
| ||||
Posted in reply to rcorre Attachments: | On Sun, 14 Jun 2015 12:26:52 +0000, rcorre wrote:
> Suppose I have a function defined like so:
>
> void foo(int i) { }
>
> intended to be called like:
>
> 5.foo
>
> Should it be labeled with @property?
> Or is @property only for true member functions?
only if you plan to use it like `foo = 5;`. i.e. exactly like field/ variable. compiler will not complain, but putting `@property` here is stylistically wrong.
|
June 14, 2015 Re: @property on free function for UFCS? | ||||
---|---|---|---|---|
| ||||
Posted in reply to ketmar | On 06/14/2015 05:50 PM, ketmar wrote: > On Sun, 14 Jun 2015 12:26:52 +0000, rcorre wrote: > >> Suppose I have a function defined like so: >> >> void foo(int i) { } >> >> intended to be called like: >> >> 5.foo >> >> Should it be labeled with @property? >> Or is @property only for true member functions? > > only if you plan to use it like `foo = 5;`. You can use it like that anyway. > i.e. exactly like field variable. struct S{ void delegate() dg; } int main(){ S s; s.dg=(){ writeln("!"); }; s.dg(); } Now show me the UFCS way. > compiler will not complain, but putting `@property` here is > stylistically wrong. > It's neither wrong nor right. |
June 14, 2015 Re: @property on free function for UFCS? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr Attachments: | On Sun, 14 Jun 2015 18:21:39 +0200, Timon Gehr wrote: >> only if you plan to use it like `foo = 5;`. > > You can use it like that anyway. sure, but i'm talking about style, not about compiler demands. >> i.e. exactly like field variable. > > struct S{ > void delegate() dg; > } > > int main(){ > S s; > s.dg=(){ writeln("!"); }; > s.dg(); > } > > Now show me the UFCS way. i'm afraid i didn't understood you here. >> compiler will not complain, but putting `@property` here is stylistically wrong. >> > It's neither wrong nor right. yet i never saw this: struct S { int n; } S s; s.n(42); the whole concept of properties (not bolted into the compiler yet) is to emulate *fields*. so it's stylistically right to declare something as a property if one wants to use it like `foo = 42;`. that means `mymodule.foo = 42;` actually. yet `42.foo` means `42.module.foo`, which even looks wrong. |
June 14, 2015 Re: @property on free function for UFCS? | ||||
---|---|---|---|---|
| ||||
Posted in reply to rcorre | On Sunday, 14 June 2015 at 12:53:43 UTC, rcorre wrote: > Is there a good reference for the current state of @property? > I know it was hotly debated for awhile (and maybe still is?). > I'm just never sure when I should be using it (if at all). Oh yes: http://wiki.dlang.org/Property_Discussion_Wrap-up |
Copyright © 1999-2021 by the D Language Foundation