www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - property on free function for UFCS?

reply "rcorre" <ryan rcorre.net> writes:
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?
Jun 14 2015
next sibling parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
You can use  property there, but you don't have to because you 
can call it with optional parenthesis anyway.
Jun 14 2015
parent reply "rcorre" <ryan rcorre.net> writes:
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).
Jun 14 2015
next sibling parent "Adam D. Ruppe" <destructionator gmail.com> writes:
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.
Jun 14 2015
prev sibling parent "Gary Willoughby" <dev nomad.so> writes:
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
Jun 14 2015
prev sibling parent reply ketmar <ketmar ketmar.no-ip.org> writes:
On Sun, 14 Jun 2015 12:26:52 +0000, rcorre wrote:

 Suppose I have a function defined like so:
=20
 void foo(int i) { }
=20
 intended to be called like:
=20
 5.foo
=20
 Should it be labeled with  property?
 Or is  property only for true member functions?
only if you plan to use it like `foo =3D 5;`. i.e. exactly like field/ variable. compiler will not complain, but putting ` property` here is=20 stylistically wrong.=
Jun 14 2015
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
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.
Jun 14 2015
parent ketmar <ketmar ketmar.no-ip.org> writes:
On Sun, 14 Jun 2015 18:21:39 +0200, Timon Gehr wrote:

 only if you plan to use it like `foo =3D 5;`.
=20 You can use it like that anyway.
sure, but i'm talking about style, not about compiler demands.
 i.e. exactly like field variable.
=20 struct S{ void delegate() dg; } =20 int main(){ S s; s.dg=3D(){ writeln("!"); }; s.dg(); } =20 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=20 emulate *fields*. so it's stylistically right to declare something as a=20 property if one wants to use it like `foo =3D 42;`. that means `mymodule.fo= o=20 =3D 42;` actually. yet `42.foo` means `42.module.foo`, which even looks=20 wrong.=
Jun 14 2015