1. Giving fucnction parameters defaults assigned values later

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).

new topic     » topic index » view message » categorize

2. Re: Giving fucnction parameters defaults assigned values later

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

new topic     » goto parent     » topic index » view message » categorize

3. Re: Giving fucnction parameters defaults assigned values later

ArthurCrump said...

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

new topic     » goto parent     » topic index » view message » categorize

4. Re: Giving fucnction parameters defaults assigned values later

mattlewis said...

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} 
 
new topic     » goto parent     » topic index » view message » categorize

5. Re: Giving fucnction parameters defaults assigned values later

DerekParnell said...
mattlewis said...

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

new topic     » goto parent     » topic index » view message » categorize

6. Re: Giving fucnction parameters defaults assigned values later

mattlewis said...

Yes. You should be able to rely on that staying true.

Matt

Thank you. I intend to make use of that.

Arthur

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu