1. Giving fucnction parameters defaults assigned values later
- Posted by ArthurCrump Jun 06, 2013
- 1456 views
In Euphoria version 4.05 a parameter default can be a variable which is only assigned a value after the function declaration. For example, the following code works when the function foo is called.
object par_default -- Currently no value -- More coding function foo(object mypay=par_default) -- some code return mypar -- Or something more useful end function -- More coding par_default = 55 -- More coding ?foo()
Can this behaviour be relied upon in future versions? I would like to put my routines before the constants which are used for the default parameter values so that the routine is documented before the constants (using creole).
2. Re: Giving fucnction parameters defaults assigned values later
- Posted by petelomax Jun 08, 2013
- 1230 views
Warning: Off topic!
Not what you asked at all, but since no one else has replied I thought I would just state that your code works on Phix and I can think of no reason why that would ever change. I even added your code to the test set to sound the alarm if it ever does. Sorry but I have not looked at how this got implemented in OpenEuphoria.
Pete
3. Re: Giving fucnction parameters defaults assigned values later
- Posted by mattlewis (admin) Jun 08, 2013
- 1224 views
In Euphoria version 4.05 a parameter default can be a variable which is only assigned a value after the function declaration. For example, the following code works when the function foo is called.
object par_default -- Currently no value -- More coding function foo(object mypay=par_default) -- some code return mypar -- Or something more useful end function -- More coding par_default = 55 -- More coding ?foo()
Can this behaviour be relied upon in future versions? I would like to put my routines before the constants which are used for the default parameter values so that the routine is documented before the constants (using creole).
I don't see any reason why this wouldn't work in the future. Top level stuff can be tricky when relying on things happening across files, because it depends on the order in which things are included. You should even be able to move the declaration of par_default down to the assignment and it should work.
Matt
4. Re: Giving fucnction parameters defaults assigned values later
- Posted by DerekParnell (admin) Jun 08, 2013
- 1238 views
I don't see any reason why this wouldn't work in the future.
I guess another way of asking this is "Is it guaranteed that the value of the default argument will always be set when the function called rather than when it is being defined?"
object DefArg function foo(object Arg = DefArg) return Arg * Arg + 1 end function DefArg = 4 ? foo() --> 17 DefArg = {1,2,3} ? foo() --> {2,5,10}
5. Re: Giving fucnction parameters defaults assigned values later
- Posted by mattlewis (admin) Jun 09, 2013
- 1172 views
I don't see any reason why this wouldn't work in the future.
I guess another way of asking this is "Is it guaranteed that the value of the default argument will always be set when the function called rather than when it is being defined?"
Yes. You should be able to rely on that staying true.
Matt
6. Re: Giving fucnction parameters defaults assigned values later
- Posted by ArthurCrump Jun 09, 2013
- 1159 views
Yes. You should be able to rely on that staying true.
Matt
Thank you. I intend to make use of that.
Arthur