View · Search · Index
No registered users in community xowiki
in last 10 minutes

Re: [Xotcl] non positional argument

From: Gustaf Neumann <neumann_at_wu-wien.ac.at>
Date: Wed, 27 Oct 2010 08:53:26 +0200

On 26.10.10 23:23, vitick_at_gmail.com wrote:
> Is it possible to have a non-positional argument that is optional and its variable is only set when it is present as the argument to the method. Otherwise, it is ignored, no error.
> Basically, I want it behave like an option to the method but without specifying any value.
...
> Or, it can even be a boolean, set to 1 if it was specified and set to 0 if not. That would be even better, shorter code.
see below for some optional argument handling. i guess you
want m2.
-gustaf neumann

=====================================
Object create myobj {
   :public method m1 {-test} {
     puts "m1 test exists [info exists test]"
   }
   :public method m2 {-test:switch} {
     puts "m2 test exists [info exists test] value $test"
   }
   :public method m3 {test:optional} {
     puts "m3 test exists [info exists test]"
   }
}

myobj m1
myobj m1 -test 1
myobj m2
myobj m2 -test
myobj m3
myobj m3 1
=====================================

output is

=====================================
m1 test exists 0
m1 test exists 1
m2 test exists 1 value 0
m2 test exists 1 value 1
m3 test exists 0
m3 test exists 1
=====================================