Re: Closures

Pat <[email protected]>
Newsgroups gmane.comp.java.beanshell.devel
Message-ID <[email protected]>
On Sat, Apr 24, 2004 at 12:47:01PM -0400, Harish Krishnaswamy wrote:
> I agree that the code will become hard to read when you add a lot 
> Perl-ish syntax, but there is a lot of other intuituve syntax that is 

BeanShell's primary proposition has always been to take the Java syntax and
then extend it in a natural way into the scripting domain.  I think the key
word there is "extend" (as in, not break, but add to).  So, as long as a syntax
does not explicitly conflict with Java grammar and as long as it is compelling
and easy to understand I see no reason not to consider it. 

BeanShell has method closure of course...  but I've been playing around with
the idea of adding first class closures for a long time.  The internals of
BeanShell make this pretty trivial...  again, it's just a matter of finding an
appealing and useful syntax.

In this case, I think the Java block syntax is a natural place to look...  Java
has no notion of blocks appearing in either assignments or in arguments, e.g.:

  code = { ... };
  someMethod( { ... } );

The same applies to method declarations:

  method = foo(a,b) { };
  someMethod( foo(a,b) { } );

I think it would be reasonable to use these as two forms of first class
closures... one taking any values from the namespace in which it's evaluated
and one taking explicit arguments as in a method call.

This syntax might also take the place of named arguments (another feature
request), as you could use an anonymous closure as a set of named arguments,
e.g.:

  someMethod( { name="Joe"; address="1234 Foo St." } );

or they could be unified in some way (use the named argument syntax with
regular commas, but internally treat it as a closure).

This might make a lot of interesting applications possible.  One question I
have though is what ever happened to the Java compact array declaration syntax
that was supposed to be coming...  I haven't seen evidence of it in 1.5.
Wasn't that going to use the { } syntax for array decs?

Also, on a slight tangent - Has anyone used the this.caller construct or other
advanced namespace features of BeanShell?  e.g. setNameSpace().  I think this
is something that is pretty advanced for a scripting language.


Thanks,
Pat



-------------------------------------------------------
This SF.net email is sponsored by: The Robotic Monkeys at ThinkGeek
For a limited time only, get FREE Ground shipping on all orders of $35
or more. Hurry up and shop folks, this offer expires April 30th!
http://www.thinkgeek.com/freeshipping/?cpg=12297
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.