Passing a function as a parameter to another function

Is it possible to pass a function into another function as a parameter:


on f1()
	set n to 1
end f1

on f2(a)
	a()
end f2

f2(f1)

No, arguments are always local variables and they cannot reach out of the handler’s scope. Therefore the handler constant cannot be used from another handler. To pass different handlers you can use different script objects containing the same handler constants, pretty much like a protocol.

edit: Example:

script a
	on default()
		return "a"
	end default
end script

script b
	on default()
		return "b"
	end default
end script

doAction(a)
doAction(b)

on doAction(CBObject)
	-- do your stuff here
	CBObject's default()
end doAction

I found script objects just after posting. It’s going to work for me but it’s taking me to scoping hell. I’m going to have to actually read something.

Thanks for the input. Useful

Yes:


on f1()
	set n to 1
end f1

on f2(a)
	a
end f2

my f2(my f1()) -- the best safe syntax