Handlers with Labeled Parameters

Back again in my never-ending quest to assert dominance over the Machine :smiley:

I’m messing around with handlers with labeled parameters. Here’s my test script:


to displayADialog with textTodisplay
	display dialog "" & textTodisplay
end displayADialog

set textTodisplay to "Hello"
displayADialog with textTodisplay

-- Returns a dialog showing "true" with or without setting textToDisplay to a value

Why ??? Meanwhile…


to displayADialog with textTodisplay
	display dialog "" & textTodisplay
end displayADialog

displayADialog given textTodisplay:"Hello"
-- Returns dialog showing "Hello" 

Is this stuff nonintuitive or do I have kind of disconnect where I’m not seeing the logic of these statements. Logically doesn’t it seem like if I structure the request “to displayADialog with textToDisplay” then I would call it the same way? And is there any good info on the variants usable with these handlers. [i] to findAFish under theSea[/i] :stuck_out_tongue:

Slightly Confused,
Jim

Defining a Subroutine With Labeled Parameters

Hi, Jim.

‘with’ and ‘without’ aren’t recognised handler parameter labels. You shouldn’t use them in the ‘on’ line of handler definitions.

You can, however, use them in calls to a handler that has a suitable ‘given’ parameter. In that case, they set the parameter value to ‘true’ or ‘false’ respectively.

on doSomething given TwiddlyBits:doingTwiddlyBits
	if (doingTwiddlyBits) then
		beep
	else
		beep 2
	end if
end doSomething

doSomething with TwiddlyBits --> one beep

doSomething without TwiddlyBits --> two beeps