Function names and lambdas

Russel Winder via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Apr 7 05:43:42 PDT 2017


On Thu, 2017-04-06 at 11:45 -0700, Ali Çehreli 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:

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 = int function(int);
> 
> int foo(int i) {
>      return i;
> }
> 
> void main() {
>      Func[] funcs = [ &foo ];
> }

I just did:

immutable funcs = [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 
> alias template parameter:
> 
> void bar(alias func)() {
>      func(42);
> }
> 
> int foo(int i) {
>      return i;
> }
> 
> void main() {
>      bar!foo();
> }

Good to know but for situation here the &foo was what was needed.

-- 
Russel.
=============================================================================
Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russel.winder at ekiga.net
41 Buckmaster Road    m: +44 7770 465 077   xmpp: russel at winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: This is a digitally signed message part
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20170407/2d6db199/attachment-0001.sig>


More information about the Digitalmars-d-learn mailing list