The documentation of the wren scripting language http://wren.io/ explains how to define methods within a class, but I want to define a simple function and not a method. I tried this:

#! /usr/bin/env wren
square(n) {
        return n * n
}

System.print(square(3))

and this (omitting the parts other than the attempted function definition:

var square= {|n|
        return n * n
}

and this

var square= Fn.new {|n|
        return n * n
}

but nothing worked.

Any advice how I could make it work without resorting to method definitions?

1 Answers

0
Guenther Brunthaler On

I found a solution: Obviously I forgot the invoke the call() method on the function object. This works:

#! /usr/bin/env wren
var square= Fn.new {|n|
        return n * n
}
System.print(square.call(3))

However, I find it hard to believe that all user-defined functions have to be called by explicitly invoking a "call" method on them!

The last time I have seen something like that where "call" had to be written explicitly all the time was TI BASIC for the TI-99/4a.

But that was about 35 years ago! ;-)