www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Function names and lambdas

reply Russel Winder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
I am used to a function name being a reference to the function body,
cf. lots of other languages. However D rejects:

	iterative

as a thing can put in an array, it requires:

	(n) =3D> iterative(n)

Presumably this introduces inefficiency at run time? I.e. the extra
level of indirection is not compiled away?

--=20
Russel.
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D
Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russel.winder ekiga.n=
et
41 Buckmaster Road    m: +44 7770 465 077   xmpp: russel winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder
Apr 06 2017
next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Thursday, 6 April 2017 at 18:37:51 UTC, Russel Winder wrote:
 I am used to a function name being a reference to the function 
 body, cf. lots of other languages. However D rejects:

 	iterative
Try &iterative The compiler would probably optimize out a trivial thing anyway, but &foo should work too in most cases.
Apr 06 2017
parent Russel Winder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Thu, 2017-04-06 at 18:45 +0000, Adam D. Ruppe via Digitalmars-d-
learn wrote:
 On Thursday, 6 April 2017 at 18:37:51 UTC, Russel Winder wrote:
 I am used to a function name being a reference to the function=C2=A0
 body, cf. lots of other languages. However D rejects:
=20
 	iterative
=20 Try =20 &iterative =20 =20 =20 The compiler would probably optimize out a trivial thing anyway,=C2=A0 but &foo should work too in most cases.
No don't I look like a real idiot. Of course I should have known that. :-) It works exactly as required. --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder ekiga.n= et 41 Buckmaster Road m: +44 7770 465 077 xmpp: russel winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
Apr 07 2017
prev sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 04/06/2017 11:37 AM, Russel Winder via Digitalmars-d-learn wrote:
 I am used to a function name being a reference to the function body,
 cf. lots of other languages. However D rejects:

 	iterative

 as a thing can put in an array, it requires:

 	(n) => iterative(n)

 Presumably this introduces inefficiency at run time? I.e. the extra
 level of indirection is not compiled away?
I think it's just a design choice. C implicitly converts the name of the function to a pointer to that function. D requires the explicit & operator: alias Func = int function(int); int foo(int i) { return i; } void main() { Func[] funcs = [ &foo ]; } Close to what you mentioned, name of the function can be used as an alias template parameter: void bar(alias func)() { func(42); } int foo(int i) { return i; } void main() { bar!foo(); } Ali
Apr 06 2017
next sibling parent Russel Winder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Thu, 2017-04-06 at 11:45 -0700, Ali =C3=87ehreli via Digitalmars-d-learn
wrote:
=20
[=E2=80=A6]
 I think it's just a design choice. C implicitly converts the name of
 the=C2=A0
 function to a pointer to that function. D requires the explicit &
 operator:
One of the dangers of being a bit like and a replacement for another language is that often people carry ideas over incorrectly, as I have here.
 alias Func =3D int function(int);
=20
 int foo(int i) {
 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0return i;
 }
=20
 void main() {
 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0Func[] funcs =3D [ &foo ];
 }
I just did: immutable funcs =3D [tuple(&foo, "foo")]; as I don't need the name of the type, but I do need a string form of the name of the function.
 Close to what you mentioned, name of the function can be used as an=C2=A0
 alias template parameter:
=20
 void bar(alias func)() {
 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0func(42);
 }
=20
 int foo(int i) {
 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0return i;
 }
=20
 void main() {
 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0bar!foo();
 }
Good to know but for situation here the &foo was what was needed. --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder ekiga.n= et 41 Buckmaster Road m: +44 7770 465 077 xmpp: russel winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
Apr 07 2017
prev sibling parent reply Yuxuan Shui <yshuiv7 gmail.com> writes:
On Thursday, 6 April 2017 at 18:45:26 UTC, Ali Çehreli wrote:
 On 04/06/2017 11:37 AM, Russel Winder via Digitalmars-d-learn 
 wrote:
[...]
I think it's just a design choice. C implicitly converts the name of the function to a pointer to that function. D requires the explicit & operator: alias Func = int function(int); int foo(int i) { return i; } void main() { Func[] funcs = [ &foo ]; } Close to what you mentioned, name of the function can be used as an alias template parameter: void bar(alias func)() { func(42); } int foo(int i) { return i; } void main() { bar!foo(); } Ali
Main reason is probably UFCS.
Apr 07 2017
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 04/07/2017 11:19 AM, Yuxuan Shui wrote:
 On Thursday, 6 April 2017 at 18:45:26 UTC, Ali Çehreli wrote:
 On 04/06/2017 11:37 AM, Russel Winder via Digitalmars-d-learn wrote:
 [...]
I think it's just a design choice. C implicitly converts the name of the function to a pointer to that function. D requires the explicit & operator: alias Func = int function(int); int foo(int i) { return i; } void main() { Func[] funcs = [ &foo ]; } Close to what you mentioned, name of the function can be used as an alias template parameter: void bar(alias func)() { func(42); } int foo(int i) { return i; } void main() { bar!foo(); } Ali
Main reason is probably UFCS.
Main reason for D not supporting the name-to-pointer mapping? I don't think so because as far as I know this has been the case since very early on but UFCS came very much later. Ali
Apr 07 2017
parent Jacob Carlborg <doob me.com> writes:
On 2017-04-07 23:05, Ali Çehreli wrote:

 Main reason for D not supporting the name-to-pointer mapping? I don't
 think so because as far as I know this has been the case since very
 early on but UFCS came very much later.
More likely due to properties, i.e. calling functions without parentheses. It's more difficult to know if the function should be called or if the address of the function should be taken. -- /Jacob Carlborg
Apr 08 2017