de en es fr
Let the machine help
Light teasing, exhibition, BDSM, sissyfication, watersports... with sounds and pictures


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 calling closures
PostPosted: Mon Oct 01, 2018 10:11 pm 
Offline
Active member
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


Top
 Profile Send private message 
 
 Re: calling closures
PostPosted: Tue Oct 02, 2018 10:39 am 
Offline
Site Admin
User avatar
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()


Top
 Profile Send private message 
 
 Re: calling closures
PostPosted: Tue Oct 02, 2018 12:15 pm 
Offline
Active member
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.


Top
 Profile Send private message 
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC + 1 hour [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  


Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Maroon Fusion theme created by Oxydo
Software, theme modifications, phpBB modification by Doti 2010 - 2020
This website uses session cookies only.