Re: Re: Curious about method handling

James Reeves <[email protected]> Fri, 5 Aug 2005 21:28:23 +0100
Newsgroups gmane.comp.lang.nice.general
Message-ID <[email protected]>
On Friday 05 Aug 2005 18:55, Artem Gr wrote:
> The question was:
>
> "Also, is there any way of passing a class method as an argument without
> wrapping it in an anonymous method?"
>
> To wich the (not tested) example is:
>
> // Pass the A.g class method to the repeater.
> repeatOnA( new A(), g );

I think I'd better first clear up some of the confusion my original post has 
left in its wake.

I was curious as to whether there was a syntax I was unaware of that I could 
use to refer to a method of a class, without resorting to anonymous method 
wrappers. In Python, for instance, I can do this:

class A:
  def __init__(greeting):
    self.greeting = greeting
  def greet():
    return greeting

def print_output(func):
  print func()

print_output( A("Hello World").greet )

This prints "Hello World" to the screen. The greet. method remembers which 
object it belongs to when it is passed to print_output.

In Nice, we can do something similar, but it seems we need to wrap our object 
method in an anonymous method:

class A {
  String greeting;
  String greet() = this.greeting;
}

new A(String s) {
  this(greeting: s);
}

void printOutput( void->String func ) {
  println( func() );
}

void main(String[] args) {
  printOutput( () => { return new A("Hello World").greet(); } );
}

There is another method to achieve this, which other helpful people have 
suggested:

class A {
  String greeting;
  String greet() = this.greeting;
}

new A(String s) {
  this(greeting: s);
}

void printOutput(A klass, A->String func ) {
  println( func(klass) );
}

void main(String[] args) {
  printOutput( new A("Hello World"), greet );
}

This appears neater, but it contains a superfluous type. The compiler doesn't 
need to know the class of the object the method belongs to. All that should 
be necessary to know is the input types and output type of the method.

Given the above example, I can't do this without coming up against a type 
error:

class B {
  String farewell;
  String bye() = this.farewell;
}

new B(String s) {
  this(farewell: s);
}

void main(String[] args) {
  printOutput( new B("Hello World"), bye );
}

So I wonder if a slight change in syntax would be feasible.

If "className.methodName" returned the method itself, not the return value of 
the method, it would make it consistant with the syntax for non-class 
methods.

So I could do this:
printOutput(new A("Hello World").greet)

This syntax would be particularly useful for lookup tables that refer to 
multiple classes. e.g:
[
  (KeyEvent.VK_LEFT, Spaceship.moveLeft),
  (KeyEvent.VK_RIGHT, Spaceship.moveRight),
  (KeyEvent.VK_P, Game.pause)
  (KeyEvent.VK_ESCAPE, Game.quit)
].listToMap();

Is there any reason I don't know about that would make altering the syntax 
like this this a bad idea? Is there any benefit to having class.method === 
class.method() ?

--
James Reeves
http://www.monkeyengines.co.uk/


-------------------------------------------------------
SF.Net email is Sponsored by the Better Software Conference & EXPO
September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices
Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA
Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf