Sign in to follow this  

Functors vs Delegates

This topic is 3591 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In trying to expand my programming horizons I've been looking into delegates lately. I've previously used functors with good success and it strikes me that there are many parallels between what these two methodologies offer... but what is 'best practice usage' for them (programming in C++) and why should I prefer one over the other (or should I not)? Any and all constructive advice is appreciated. Thanks, Timkin

Share this post


Link to post
Share on other sites
"Functor" is a C++-specific word meaning "something (usually an object) that acts like a function". One major thing functors are used for is simulating partial application, aka function parameter binding.

"Delegate" is a C#-specific word meaning "a member function partially applied with an object". Delegates are often used in the same places functors are used, as callback functions with attached state. These days, delegates have leaked into other languages, including C++. What I love delegates for is an easy, low-syntax, low-commitment version of virtual interfaces. C++ in its basic form encourages people to implement the subject/observer (aka listener) pattern incorrectly. With delegates, doing it correctly is just as easy.

If you're looking into functors and delegates, what you should really be looking into is closures, a concept which subsumes the delegate concept as well as the most common use of the functor idiom. It's not C++, but it'll serve as a good jumping-off point for this area of thought.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
"Delegate" is a C#-specific word meaning "a member function partially applied with an object".


Could you expand on that definition please... perhaps with a usage example if you have the time.

Thanks,

Timkin

Share this post


Link to post
Share on other sites
It's a big can of worms, and I've cruelly used a definition which hits you with two big shotgun barrels of functional programming instead of making it concise and clear.. I wrote up a big post explaining, but I wasn't really satisfied with it. Instead, I present you with this snippet of code written in Lua to whet your appetite, and a book recommendation to follow once I remember the title.


function add(a, b)
return a+b
end

-- for contrived reasons, I need a lot of specific adders

function add3(a)
return add(a, 3)
end

function add4(a)
return add(a, 4)
end

-- man, all these are getting annoying

function makeAddFunc(b)
return (function(a) return a+b end)
end

add5 = makeAddFunc(5)
add6 = makeAddFunc(6)

add5(3) -- returns 8
add6(3) -- returns 9


function describeShape(shape, color)
print("A " .. shape .. " which is " .. color);
end

function exampleShapes(f)
f("square", "blue");
f("triangle", "green");
end

exampleShapes(describeShape);
-- prints "a square which is blue" and "a triangle which is green"

-- now, actual graphics, instead of text
w = createWindow("my window");

w:drawShape("square", "green");
w:drawShape("circle", "green");

exampleShapes(w:drawShape) -- whoops! doesn't work-- not real syntax (wouldn't work in C++ either)

delegate = function(shape, color) w:drawShape(square, color) end
exampleShapes(delegate) -- this works

Share this post


Link to post
Share on other sites
So if I interpreted your last post correctly Sneftel a delegate is then, essentially, a functional wrapper for a member function with specified binding to an instantiated object...

...and presumably for a given delegate you could rebind both the member function (assuming invariant signature) and the specific object at runtime...

... so any time where this behaviour was required, along with a thin interface to member functions (such as rebindable callbacks) it would make sense to use a delegate? Or not?

Share this post


Link to post
Share on other sites
Quote:
Original post by Timkin
So if I interpreted your last post correctly Sneftel a delegate is then, essentially, a functional wrapper for a member function with specified binding to an instantiated object...
Yes.
Quote:
...and presumably for a given delegate you could rebind both the member function (assuming invariant signature) and the specific object at runtime...

The binding is performed at runtime, yes. After creation, a delegate is generally not rebound; one simply creates a new delegate with the desired binding. (That assumes that objects are held by reference, of course; if they're held by value there's more reason to be able to do that, via operator=.)
Quote:
... so any time where this behaviour was required, along with a thin interface to member functions (such as rebindable callbacks) it would make sense to use a delegate? Or not?
Yep, that's about right.

Share this post


Link to post
Share on other sites

This topic is 3591 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this