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

Just starting out? Need help? Post your questions and find answers here.
User avatar
SparrowhawkMMU
Posts: 291
Joined: Wed Aug 19, 2015 3:02 pm
Location: United Kingdom

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

Post 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.
Last edited by SparrowhawkMMU on Fri Feb 12, 2016 3:49 pm, edited 1 time in total.
User avatar
SparrowhawkMMU
Posts: 291
Joined: Wed Aug 19, 2015 3:02 pm
Location: United Kingdom

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

Post by SparrowhawkMMU »

To clarify: "foo is undefined" is shown when I click the "Error" button
Fred
Site Admin
Posts: 1821
Joined: Mon Feb 24, 2014 10:51 am

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

Post 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.
User avatar
SparrowhawkMMU
Posts: 291
Joined: Wed Aug 19, 2015 3:02 pm
Location: United Kingdom

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

Post by SparrowhawkMMU »

Ah OK. That makes sense - thanks for the clarification
Post Reply