www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - delegates vs functions => practical consequences

reply "Xan" <xancorreu gmail.com> writes:
Hi,

I want to know what is most interesting for me: delegates or 
functions. I consulted sources but none say the practical 
consequences of such election.

What can I do and what can't I do with functions and delegates?
Please, be didactics, I'm a newbee

Thanks,
Xan.
Apr 18 2012
next sibling parent "John Chapman" <johnch_atms hotmail.com> writes:
On Wednesday, 18 April 2012 at 21:07:08 UTC, Xan wrote:
 Hi,

 I want to know what is most interesting for me: delegates or 
 functions. I consulted sources but none say the practical 
 consequences of such election.

 What can I do and what can't I do with functions and delegates?
 Please, be didactics, I'm a newbee

 Thanks,
 Xan.
Well, function pointers can point to static functions, delegates cannot. Delegates can point to class/struct methods, function pointers cannot. Just use whichever is appropriate for your needs. To convert a function pointer to a delegate, call std.functional.toDelegate(). See http://dlang.org/function.html#closures
Apr 18 2012
prev sibling next sibling parent reply Mirko Pilger <pilger cymotec.de> writes:
 I want to know what is most interesting for me: delegates or functions.
 I consulted sources but none say the practical consequences of such
 election.
in addition to what john said: regarding _function literals_ the difference is by using a delegate instead of a function you have access to the enclosing frame, e.g. variables in the same scope. the following code doesn't compile with the error "[...] cannot access frame of [...]": int y; auto fn= function(int x) {return x+y;}; if you change this to: int y; auto fn= delegate(int x) {return x+y;}; it compiles without error.
Apr 18 2012
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 04/18/2012 03:50 PM, Mirko Pilger wrote:
 I want to know what is most interesting for me: delegates or functions.
 I consulted sources but none say the practical consequences of such
 election.
in addition to what john said: regarding _function literals_ the difference is by using a delegate instead of a function you have access to the enclosing frame, e.g. variables in the same scope. the following code doesn't compile with the error "[...] cannot access frame of [...]": int y; auto fn= function(int x) {return x+y;}; if you change this to: int y; auto fn= delegate(int x) {return x+y;}; it compiles without error.
Additionally, thanks to a recent bug fix, omitting 'function' and 'delegate' will deduce the right one. Both of the following are delegates because they access the variable 'y' from the enclosing scope: auto fn_1 = (int x) {return x+y;}; auto fn_2 = (int x) => x + y; The latter syntax doesn't help much in this context but still... Ali
Apr 18 2012
prev sibling next sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Wed, Apr 18, 2012 at 11:07:07PM +0200, Xan wrote:
 Hi,
 
 I want to know what is most interesting for me: delegates or
 functions. I consulted sources but none say the practical
 consequences of such election.
 
 What can I do and what can't I do with functions and delegates?
 Please, be didactics, I'm a newbee
[...] The difference between functions and delegates is that functions do not have access to variables and other stuff declared in their containing lexical scope, whereas delegates do. For example: auto outer1(int[] args) { int x, y; return function(int z) { //z += args[0]; // Illegal: function has no // acceses to args of outer // function //return x; // Illegal: function has no // local vars in outer function return z+1; // OK, function can access its // own parameters } } auto outer2(int[] args) { int x, y; return delegate(int z) { z += args[0]; // OK, delegates contain a // "hidden pointer" to their // containing context z += x; // This access includes outer // function's local variables return y+1; // OK } } In other words, delegates are much more flexible and powerful. However, they come at a cost: function pointers are just a bare pointer to some code, so they are very cheap. Delegates, on the other hand, are "fat" pointers: in addition to the pointer to code, they also come with a (hidden) pointer to the surrounding context of the delegate. Furthermore, if you're returning a delegate (like in the example above), this may cause some local variables in the outer function to be moved out of the runtime stack into the heap, because otherwise the delegate's context pointer will be pointing to invalid memory after the outer function returns. This may have some speed/memory usage consequences. Bare function pointers do not have this tradeoff. T -- Sometimes the best solution to morale problems is just to fire all of the unhappy people. -- despair.com
Apr 18 2012
prev sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Wed, 18 Apr 2012 17:07:07 -0400, Xan <xancorreu gmail.com> wrote:

 Hi,

 I want to know what is most interesting for me: delegates or functions.  
 I consulted sources but none say the practical consequences of such  
 election.

 What can I do and what can't I do with functions and delegates?
 Please, be didactics, I'm a newbee
In my experience, delegates are the more useful type to *store*. I've implemented this in some places: int delegate(int) stored_dg; void setDelegate(int delegate(int) dg) { stored_dg = dg; } void setDelegate(int function(int) fn) { stored_dg = std.functional.toDelegate(fn); } On the whole, delegates are slightly more expensive to call, but not by much. However, a function converted to a delegate pays the penalty of a double call, because it takes a call to strip out the context pointer. I wish there was a more straightforward way to do this... But I've not seen this be a huge factor -- yet. -Steve
Apr 19 2012
parent reply "Xan" <xancorreu gmail.com> writes:
On Thursday, 19 April 2012 at 11:46:59 UTC, Steven Schveighoffer 
wrote:
 On Wed, 18 Apr 2012 17:07:07 -0400, Xan <xancorreu gmail.com> 
 wrote:

 Hi,

 I want to know what is most interesting for me: delegates or 
 functions. I consulted sources but none say the practical 
 consequences of such election.

 What can I do and what can't I do with functions and delegates?
 Please, be didactics, I'm a newbee
In my experience, delegates are the more useful type to *store*. I've implemented this in some places: int delegate(int) stored_dg; void setDelegate(int delegate(int) dg) { stored_dg = dg; } void setDelegate(int function(int) fn) { stored_dg = std.functional.toDelegate(fn); } On the whole, delegates are slightly more expensive to call, but not by much. However, a function converted to a delegate pays the penalty of a double call, because it takes a call to strip out the context pointer. I wish there was a more straightforward way to do this... But I've not seen this be a huge factor -- yet. -Steve
Thank you very much all of you for the information. Now I have an idea of practical benefits and contra-benefits of these. By the other hand, is there toFunction for passing delegate to function (theorically it's possible, isn't?) Thanks, Xan.
Apr 19 2012
parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Thu, 19 Apr 2012 09:04:47 -0400, Xan <xancorreu gmail.com> wrote:

 Thank you very much all of you for the information. Now I have an idea  
 of practical benefits and contra-benefits of these.

 By the other hand, is there toFunction for passing delegate to function  
 (theorically it's possible, isn't?)
No. A delegate requires a context pointer. you *can* extract the actual function pointer and context pointer from a delegate, but you can't call the function without making a delegate out of it again. -Steve
Apr 19 2012