www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Equivalent of C++ std::function

reply "Yuushi" <ShinomoriAoshi13 gmail.com> writes:
I was wondering if D had something akin to std::function in C++.

Say I have some functions in an associative array, for example:

      auto mapping = ['!' : (string a) => toUpper!string(a), '^' : 
(string a) => capitalize!string(a)];

What I want to do is basically declare something like:

     function string(string) transform;
     if(<some condition>) {
          transform = mapping[<lookup>];
     }

In C++, this could be done by declaring:

      std::function<string(string)> transform;

Is there an equivalent D construct for this?
Jun 24 2014
parent reply Mike Parker <aldacron gmail.com> writes:
On 6/25/2014 10:10 AM, Yuushi wrote:
 I was wondering if D had something akin to std::function in C++.

 Say I have some functions in an associative array, for example:

       auto mapping = ['!' : (string a) => toUpper!string(a), '^' :
 (string a) => capitalize!string(a)];

 What I want to do is basically declare something like:

      function string(string) transform;
      if(<some condition>) {
           transform = mapping[<lookup>];
      }

 In C++, this could be done by declaring:

       std::function<string(string)> transform;

 Is there an equivalent D construct for this?
For function pointers (free functions or static class functions): alias TransformFunc = string function( string ); TransformFunc transform; if( foo ) transform = &func; For delegates (lambdas or pointers to class methods or inner functions): alias TransformDg = string delegate( string ); TransformDG transform; if( foo ) transform = &bar.method; --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com
Jun 24 2014
parent reply Mike Parker <aldacron gmail.com> writes:
On 6/25/2014 10:45 AM, Mike Parker wrote:
 On 6/25/2014 10:10 AM, Yuushi wrote:
 I was wondering if D had something akin to std::function in C++.

 Say I have some functions in an associative array, for example:

       auto mapping = ['!' : (string a) => toUpper!string(a), '^' :
 (string a) => capitalize!string(a)];

 What I want to do is basically declare something like:

      function string(string) transform;
      if(<some condition>) {
           transform = mapping[<lookup>];
      }

 In C++, this could be done by declaring:

       std::function<string(string)> transform;

 Is there an equivalent D construct for this?
For function pointers (free functions or static class functions): alias TransformFunc = string function( string ); TransformFunc transform; if( foo ) transform = &func; For delegates (lambdas or pointers to class methods or inner functions): alias TransformDg = string delegate( string ); TransformDG transform; if( foo ) transform = &bar.method;
And in this case you want the latter: TransformDg transform = mapping[ '!' ]; --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com
Jun 24 2014
parent reply "Yuushi" <ShinomoriAoshi13 gmail.com> writes:
On Wednesday, 25 June 2014 at 01:47:13 UTC, Mike Parker wrote:
 On 6/25/2014 10:45 AM, Mike Parker wrote:
 On 6/25/2014 10:10 AM, Yuushi wrote:
 I was wondering if D had something akin to std::function in 
 C++.

 Say I have some functions in an associative array, for 
 example:

      auto mapping = ['!' : (string a) => toUpper!string(a), 
 '^' :
 (string a) => capitalize!string(a)];

 What I want to do is basically declare something like:

     function string(string) transform;
     if(<some condition>) {
          transform = mapping[<lookup>];
     }

 In C++, this could be done by declaring:

      std::function<string(string)> transform;

 Is there an equivalent D construct for this?
For function pointers (free functions or static class functions): alias TransformFunc = string function( string ); TransformFunc transform; if( foo ) transform = &func; For delegates (lambdas or pointers to class methods or inner functions): alias TransformDg = string delegate( string ); TransformDG transform; if( foo ) transform = &bar.method;
And in this case you want the latter: TransformDg transform = mapping[ '!' ]; --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com
Thanks a ton - I guess I need to do a fair bit more reading about alias.
Jun 24 2014
next sibling parent reply "Olivier Pisano" <olivier.pisano laposte.net> writes:
On Wednesday, 25 June 2014 at 03:33:15 UTC, Yuushi wrote:
 Thanks a ton - I guess I need to do a fair bit more reading 
 about alias.
In this case, alias acts as typedef in C++. What is important here is the function pointers/delegates syntax.
Jun 24 2014
parent reply "Yuushi" <ShinomoriAoshi13 gmail.com> writes:
On Wednesday, 25 June 2014 at 05:13:00 UTC, Olivier Pisano wrote:
 On Wednesday, 25 June 2014 at 03:33:15 UTC, Yuushi wrote:
 Thanks a ton - I guess I need to do a fair bit more reading 
 about alias.
In this case, alias acts as typedef in C++. What is important here is the function pointers/delegates syntax.
Yeah, I realised that when I went back and looked at what I'd tried: function string(string) transform; which should have been: string function(string) transform; which does work. Thanks for the clarification.
Jun 24 2014
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 06/24/2014 11:23 PM, Yuushi wrote:

 Yeah, I realised that when I went back and looked at what I'd tried:

       function string(string) transform;
That gets me all the time! :-/ That is the long version of the function literal syntax: auto f = function string(string s) { return "hello"; };
 which should have been:

      string function(string) transform;

 which does work.

 Thanks for the clarification.
Ali
Jun 25 2014
prev sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 06/24/2014 08:33 PM, Yuushi wrote:

 I guess I need to do a fair bit more reading about alias.
It is probably too basic for you but somebody else may find it useful: http://ddili.org/ders/d.en/lambda.html Ali
Jun 24 2014