Re: Curious about method handling

Artem Gr <[email protected]> Fri, 05 Aug 2005 00:18:45 +0400
Newsgroups gmane.comp.lang.nice.general
Message-ID <[email protected]>
> Hello there!

Salute!

> So I'm rather impressed, but I do have a few questions. Some syntax about 
> methods confuses me. I think it's best explained with an example:
> 
> String f() = "Hello World";
> 
> class A {
>   String greeting = "Hello World";
>   String g() = this.greeting;
> }
> 
> void main(String[] args) {
>   println( f );
>   println( ( new A() ).g );
> }
> 
> The former command prints "#<procedure f>", whilst the latter prints "Hello 
> World". Why the difference in syntax between class methods and non-class 
> methods?
> 
> Also, is there any way of passing a class method as an argument without 
> wrapping it in an anonymous method?

Sure.
Continuing your example...

/** Invoke a standalone method. */
void repeatSomething( ()->String action, int count = 99 ){
   while( count-- > 0 ) println( action() );
}

// Should print the result of the f() method 99 times.
repeatSomething( f );

/** Invoke one of the A methods. */
void repeatOnA( A a, (A)->String action, int count = 99 ){
   while( count-- > 0 ) println( action( a ) );
}

// Should print the result of the A#g() method 99 times;
repeatOnA( new A(), A.g );


-------------------------------------------------------
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