Using method closures...

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Using method closures...

Furash Gary
Message
I know Beanshell trivially supports method closures (things that return a "this" reference).  Closures are supposed to be "cool" (i.e., useful).
 
Does anyone have any examples of situations where they are useful?
 
Gary Furash, Applications Manager, Maricopa County Attorney's Office
 
Reply | Threaded
Open this post in threaded view
|

Re: Using method closures...

patniemeyer
> I know Beanshell trivially supports method closures (things that return
> a "this" reference).  Closures are supposed to be "cool" (i.e., useful).
>
> Does anyone have any examples of situations where they are useful?

I think that the "cool" stuff generally centers around what I guess you'd
call anonymous closures...  and utilities that accept those as part of
their API.

What that means is the ability to basically pass a chunk of code as an
argument to a method and have the method evaluate it when it needs it,
with various kinds of control over binding variables to it...

BeanShell doesn't currently have a syntax for making these kinds of
closures...  only named ones (the return 'this' kind) that are bound into
the namespace in which they were created).  There is nothing technically
difficult about doing this and in fact BeanShell has all the tools to do
it... you could sort of roll your own using the setNameSpace() or
namespace setParent() methods and the this.caller reference.  But that's
not friendly of course.

We will have something on this front soon.


Pat




-------------------------------------------------------
SF.Net email is sponsored by: Discover Easy Linux Migration Strategies
from IBM. Find simple to follow Roadmaps, straightforward articles,
informative Webcasts and more! Get everything you need to get up to
speed, fast. <a href="http://ads.osdn.com/?ad_idt77&alloc_id492&op=click">http://ads.osdn.com/?ad_idt77&alloc_id492&op=click
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users
Reply | Threaded
Open this post in threaded view
|

RE: Using method closures...

Furash Gary
In reply to this post by Furash Gary
I added your commments from an earlier post re anon closures to the
enhancement page.

-----Original Message-----
From: Pat Niemeyer [mailto:[hidden email]]
Sent: Wednesday, June 22, 2005 7:22 AM
To: Furash Gary
Cc: [hidden email]
Subject: Re: [Beanshell-users] Using method closures...


> I know Beanshell trivially supports method closures (things that
> return a "this" reference).  Closures are supposed to be "cool" (i.e.,

> useful).
>
> Does anyone have any examples of situations where they are useful?

I think that the "cool" stuff generally centers around what I guess
you'd call anonymous closures...  and utilities that accept those as
part of their API.

What that means is the ability to basically pass a chunk of code as an
argument to a method and have the method evaluate it when it needs it,
with various kinds of control over binding variables to it...

BeanShell doesn't currently have a syntax for making these kinds of
closures...  only named ones (the return 'this' kind) that are bound
into the namespace in which they were created).  There is nothing
technically difficult about doing this and in fact BeanShell has all the
tools to do it... you could sort of roll your own using the
setNameSpace() or namespace setParent() methods and the this.caller
reference.  But that's not friendly of course.

We will have something on this front soon.


Pat




-------------------------------------------------------
SF.Net email is sponsored by: Discover Easy Linux Migration Strategies
from IBM. Find simple to follow Roadmaps, straightforward articles,
informative Webcasts and more! Get everything you need to get up to
speed, fast. <a href="http://ads.osdn.com/?ad_idt77&alloc_id492&op=click">http://ads.osdn.com/?ad_idt77&alloc_id492&op=click
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users
Reply | Threaded
Open this post in threaded view
|

Re: Using method closures...

Daniel Wunsch
In reply to this post by Furash Gary
On Wednesday 22 June 2005 16:01, Furash Gary wrote:
> I know Beanshell trivially supports method closures (things that return
> a "this" reference).  Closures are supposed to be "cool" (i.e., useful).
>
> Does anyone have any examples of situations where they are useful?

the best example: you need a good, old-fashioned data-structure.

Data(String foo, int bar) { return this; }

if you need Data with methods later, just put in some functions.

Data(String foo, int bar) {
    String asString() { return foo + "/" + bar; }
}

you just have to remember to use "super" in these when you
mean attributes of the Data object instead of "this". "this"
within asString would mean then scope of asString, not of
Data!

i seldomly need objects with more than one method
to construct  - so i just use a beanshell closure instead.

compare this:

class Test {
  String foo;
  int bar;

  void Test(String foo, int bar) {
    // here, "this" works differently than in a closure!
    this.foo = foo;
    this.bar = bar;
  }

  String asString() {
    return foo + " " + bar;
  }
}

with this:

Test(String foo, int bar) {
  String asString() {
    return foo + " " + bar;
  }
  return this;
}

daniel


-------------------------------------------------------
SF.Net email is sponsored by: Discover Easy Linux Migration Strategies
from IBM. Find simple to follow Roadmaps, straightforward articles,
informative Webcasts and more! Get everything you need to get up to
speed, fast. http://ads.osdn.com/?ad_id=7477&alloc_id=16492&op=click
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users