SexScripts : calling closures - https://ss.deviatenow.com:443/viewtopic.php?f=4&t=794 | Page 1 of 1 |
calling closures |
unnamed007 [ Mon Oct 01, 2018 10:11 pm ] |
---|---|
I'm having issues with closures/"methods" in groovy. The way they don't let you call them unless they're already defined is highly upsetting. Code: def pageOne = { show("You are on page one."); pageTwo() } def pageTwo = { show("You are on page two."); } pageOne() The above code will not work because I'm calling pageTwo before it's defined. If I moved it around, it would work fine. Code: def pageTwo = { show("You are on page two."); } def pageOne = { show("You are on page one."); pageTwo() } pageOne() But then what if I want to go to page one again? Code: def pageTwo = { show("You are on page two."); pageOne() } def pageOne = { show("You are on page one."); pageTwo() } pageOne() Then it's an issue again. I tried going around it by having a dynamic method, such as Code: def goToPage = { page -> switch (page) { case 0: break; case 1: show("You are on page one.") break; case 2: show("You are on page two.") break; } } goToPage(1); And this works fine, until you want to switch pages from within the page closure. Code: def goToPage = { page -> switch (page) { case 0: break; case 1: show("You are on page one.") goToPage(2); break; case 2: show("You are on page two.") break; } } goToPage(1); after some fumbling about, this is a solution that I found, but it's extremely cumbersome. Code: def pageToGoTo = 1; def pagesEnded = false; def goToPage = { page -> switch (page) { case 0: break; case 1: showButton("You are on page one.") pageToGoTo = 2; break; case 2: showButton("You are on page two.") pageToGoTo = 3; break; case 3: showButton("You are on page three.") pageToGoTo = 1; break; } } def pageNav = { while (!pagesEnded) { goToPage(pageToGoTo); } } pageNav() In the end, I will make do with what I can come up with, but I'm curious how other script writers tackled this, or if doti himself has any ideas. Basically, I'd like to be able to declare a closure, basically promising the compiler that it's defined somewhere, and then it allowing me to use it even before its defined. I don't mess with groovy outside of sexscripts, so I'm not sure if using methods instead of closures will help, I suspect it would, but the compiler does not let me use methods in the script, I assume it's a security thing? (doti?) method attempt Code: def static doWhatever () { // doing stuff } comes up with Code: script15384246411491261545393.groovy: 42: Method definition not expected here. Please define the method at an appropriate place or perhaps try using a block/Closure instead. at line: 42 column: 3. File: script15384246411491261545393.groovy @ line 42, column 3.
def static doWhatever () { ^ 1 error |
Re: calling closures |
doti [ Tue Oct 02, 2018 10:39 am ] |
---|---|
You only need to declare before, not to define. This works : Code: def pageOne, pageTwo
pageOne = { show("You are on page one."); pageTwo() } pageTwo = { show("You are on page two."); } pageOne() |
Re: calling closures |
unnamed007 [ Tue Oct 02, 2018 12:15 pm ] |
---|---|
doti wrote: You only need to declare before, not to define. This works : Code: def pageOne, pageTwo pageOne = { show("You are on page one."); pageTwo() } pageTwo = { show("You are on page two."); } pageOne() That's exactly what I needed. Thank you doti. |
Page 1 of 1 | All times are UTC + 1 hour [ DST ] |