2

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?

Chayim Friedman
  • 47,971
  • 5
  • 48
  • 77

1 Answers1

1

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! ;-)

  • This had been discussed before: https://github.com/wren-lang/wren/issues/711. In short, Wren is classes-first and can not support this syntax. – Chayim Friedman Jan 16 '21 at 18:45
  • It does not support this ATM, but will be added. Check github, I think there is an issue, otherwise make one. – Angel O'Sphere Feb 19 '23 at 02:07