开发者

Whats wrong in this groovy code?

Hi i have the following Groovy code:

package fp;
abstract class Function
{
        public static Closure map = { action, list -> return list.collect(action) }

}

This code was taken from Groovy IBM developer works series. The file name of this code is Function same as class name(even though it is not necessary in case of Groovy). When i try to run this code as:

groovy Function.groovy 

When i run i get the following error:

Caught: groovy.lang.GroovyRuntimeExce开发者_开发百科ption: This script or class could not be run.
It should either: 
- have a main method, 
- be a JUnit test, TestNG test or extend GroovyTestCase, 
- or implement the Runnable interface.

Can any one help me with this issue?


Seems clear enough to me.

To run a Groovy script, the interpreter has to find into it some directly executable code.

It is obviously not the case of your script, that indeed loads perfectly, but can't be executed as there is no statement in it, only the declaration of an abstract class.


Indeed the source file only contains a class definition. If you want to run it as a Groovy script you must add some code that will invoke your Function.map method.

// File: Functor.groovy
package fp

abstract class Functor {
    static Closure map = { action, list -> return list.collect(action) }
}

def twelveTimes = { x -> return 12 * x }
def twelveTimesAll = Functor.map.curry(twelveTimes)
def table = twelveTimesAll([1, 2, 3, 4])
println "table: ${table}"

Now you can do $ groovy Functor.groovy to run the script.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜