Web Development

Closures (discussion)

Posted on: November 13, 2007

[Last night Nirav and I were discussing about closures and their implementation. I and Nirav often discuss tech. and I’ve been thinking since long to record those discussions. This is the beginning.]

(19:27:32) me: arre.. mereko closure samajh me aagaya.. [I understood closures]
(19:27:37) nirav: wah [gr8!] [my frnd. is always encouraging]
(19:27:44) nirav: isn’t that easy?
(19:27:53) nirav: and interesting at times?
(19:27:53) me: I read closure support in javascript
(19:27:56) nirav: yeah
(19:28:01) nirav: ECMA wala[one defined in ECMA?]
(19:28:03) me: yeah..
(19:28:38) me: to answer ur question.. PHP does’t have native support for closures.. ppl uses clumsy ways to emulate them.. which I don’t think are practicable..
(19:29:15) nirav: and how do they do it?
(19:29:16) me: may be because u can’t return a reference to a function in php..
(19:29:38) nirav: well i don’t need to return reference strictly
(19:29:43) me: they create objects for ‘closure’..
(19:29:46) nirav: i can use indirection using returning type
(19:29:59) me: didn’t get that..
(19:30:05) nirav: yeah, that’s how it is done in java as well
(19:30:06) nirav: i mean
(19:30:12) nirav: e.g.
(19:30:31) nirav: f * function(){}
(19:30:41) nirav: this function is returning function pointer to some closure
(19:30:48) nirav: or block of statement
(19:30:50) nirav: ok
(19:30:51) nirav: ?
(19:30:54) me: yeah,,
(19:30:56) nirav: now
(19:31:07) nirav: instead of returning raw pointer f*
(19:31:29) nirav: i can return a type say t => class t{ void f();}
(19:31:42) nirav: so that client of the method can get t and invoke f
(19:31:57) nirav: its indirect so ugly way of using ‘closure’
(19:32:05) me: hmm…
(19:32:10) nirav: samja? [understood?]
(19:32:14) me: 1 min..
(19:32:20) nirav: in java t would be interface
(19:32:29) nirav: with method f in it
(19:32:57) nirav: because in java you can’t return function pointers/ reference to blocks
(19:34:00) nirav: i was looking at this http://felix.sourceforge.net/
(19:34:05) nirav: sounds interesting
(19:34:41) me: in the above e.g. u return object of type ‘t’ right?
(19:34:49) nirav: yeah
(19:34:50) nirav: right
(19:35:33) nirav: where ‘t’ would be abstract class or interface to be nice
(19:35:47) me: and f() in that object uses the local variables declared in the outer function..
(19:36:23) nirav: yeah
(19:36:50) me: I’ll try that in php.. should be possible..
(19:36:54) nirav: ofcourse
(19:37:10) nirav: this is common;y employed in high level OO langs
(19:37:17) me: hmm..
(19:38:49) me: what are the problems with the method u said..?
(19:39:10) nirav: problems?
(19:39:17) nirav: problem is
(19:39:31) nirav: its pretty verbose
(19:39:50) nirav: and you have to write more and more code defeating the purpose of lambda expressions
(19:40:00) me: ok
(19:40:30) nirav: if i can directly express what i want to do in a lambda expression, i don’t need that ‘t’ and that ‘f’ declaration
(19:41:12) nirav: this way of creating closure is synthetic
(19:41:19) me: hmm..
(19:41:59) me: as in javascript support for inner functions and ability to return function references seems to be a better way..
(19:42:32) nirav: same is the case with python, ruby, scheme and many other lang which supports it
(19:42:38) me: hmm..
(19:42:58) nirav: java apparently supports it with the help of anonymous inner function but there are lot of problems


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: