Page 1 of 1

[NOT A BUG] default parameter is "undefined" for callbacks

Posted: Fri Feb 12, 2016 11:31 am
by SparrowhawkMMU
Hi Fred,

I think I have found a bug when calling a function with optional params using a callback:

I think that this is new to 1.20 but I could be wrong - I have not regression tested in 1.10 or any of the 1.20 beta releases as yet

Code: Select all

Procedure Test(foo.i = 0)
	
	If foo = 0
		Debug "foo is zero"
	Else
		Debug "foo is " + Str(foo)
	EndIf
	
EndProcedure

OpenWindow(0, 0, 0, 300, 300,"TEST", #PB_Window_ScreenCentered)
ButtonGadget(0, 100, 100, 60, 22, "Error")
BindGadgetEvent(0, @Test())

Debug "Passing in 0 to Test() ..."
Test(0)

Debug "Passing in nothing to Test() ..."
Test()
I would expect all responses to be "foo is zero"

However, the debug output is in fact:

Code: Select all

Passing in 0 to Test() ...
foo is zero
Passing in nothing to Test() ...
foo is zero
foo is undefined
So it seems that default values are ignored when callbacks are used, which means having to add extra validation to test for this eventuality.

Re: SB 1.20 - default parameter is "undefined" for callbacks

Posted: Fri Feb 12, 2016 11:32 am
by SparrowhawkMMU
To clarify: "foo is undefined" is shown when I click the "Error" button

Re: SB 1.20 - default parameter is "undefined" for callbacks

Posted: Fri Feb 12, 2016 12:44 pm
by Fred
A callback needs to respect the callback signature as said in the doc. You can't put default values to it, as default values are applied when calling explicitely a function, and doesn't work for callbacks.

Re: SB 1.20 - default parameter is "undefined" for callbacks

Posted: Fri Feb 12, 2016 3:48 pm
by SparrowhawkMMU
Ah OK. That makes sense - thanks for the clarification