TWiki . Doc . MethcallBeginnerExample

/* Please try the NoviceExamples before the BeginnerExamples
To compile:
   nicec --sourcepath=.. -a methcall.jar methcall
To run:
   java -jar methcall.jar 1000000
*/


void main(String[] args){
   let n =  toSingleInt(args);
   boolean val;

   let toggle = new Toggle();
   for(var i=1; i<n; i++) val = toggle.activate().value();
   println( toggle.activate().value() );

   let ntoggle = new NToggle(toggleTrigger: 3);
   for(var i=1; i<n; i++) val = ntoggle.activate.value;
   println( ntoggle.activate.value );
}


class Toggle {                  
   boolean state = true;        

   public boolean value() = state;
             
   public Toggle activate(){
      state = !state; 
      return this; 
   }          
}

class NToggle extends Toggle {  
   int toggleTrigger = 0;
   int count = 0;

   activate(){
      count++;
      if (count >= toggleTrigger){
         state = !state;
         count = 0;
      }
      return this;
   }
}


int toSingleInt(String[] s){
   try { return Integer.parseInt(s[0]); }
   catch (Exception e){ return 1; } }


/* Notes - language
Compare with Java at the "Win32 Language Shootout"

   println( toggle.activate().value() );
   println( ntoggle.activate.value );
When there are no parameters we don't need to use () in
method calls (we do need to use them in constructors).


*/

-- IsaacGouy - 27 Aug 2003

----- Revision r1.6 - 10 Jan 2004 - 19:04 GMT - IsaacGouy
Copyright © 1999-2003 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback.