SexScripts : Methods in groovy/sscript - https://ss.deviatenow.com:443/viewtopic.php?f=4&t=756 Page 1 of 1

Methods in groovy/sscript

vladl [ Thu Apr 12, 2018 11:33 am ]

If I look into groovy documentation a method in groovy looks like so:

Code:
int fib(int n) {
    n < 2 ? 1 : fib(n-1) + fib(n-2)
}

http://groovy-lang.org/structure.html#_methods

But with sscript this does not seem to work. The syntax check complains something about method definition not expected here.

In all the scripts I have looked methods are defined and called like so:

Code:
def xyz = { argument ->
  // do something
}

xyz("test")


Is this some kind of special sscript stuff or standard groovy stuff which I have not yet stumbled over?

Re: Methods in groovy/sscript

cgut2001 [ Thu Apr 12, 2018 3:15 pm ]

Hi vladl,

the thing you see in the scripts and your second example is a closure. This is a regular groovy-structure, see http://groovy-lang.org/closures.html.

Groovy is kind of picky when it comes to places and contexts where methods can be defined and where they can't. For example it's not possible to define them inside of other methods or closures.

Closures on the other hand may basically be defined everywhere. For the other differences between these structures like variable-scope etc. please see the documentation.

I don't know if the scripts are run within a hierachy like a closure of the main program in sexscripts. This is something doti would have to answer. But if this is the case, then methods will not work, but closures will still do. So I suggest you try to use these.


Regards

cgut2001

Re: Methods in groovy/sscript

vladl [ Thu Apr 12, 2018 5:02 pm ]

Many thanks cgut2001

Re: Methods in groovy/sscript

doti [ Thu Apr 12, 2018 5:33 pm ]

Yes, all the script code is itself inside a method, so you can only create closures

Re: Methods in groovy/sscript

cgut2001 [ Thu Apr 12, 2018 5:48 pm ]

doti wrote:
Yes, all the script code is itself inside a method, so you can only create closures


Thanks for the information, doti

Page 1 of 1 All times are UTC + 1 hour [ DST ]
https://ss.deviatenow.com:443/
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Maroon Fusion theme created by Oxydo
Software, theme modifications, phpBB modification by Doti 2010, 2011