miniscript icon indicating copy to clipboard operation
miniscript copied to clipboard

Confused function's outer

Open marcgurevitx opened this issue 2 years ago • 2 comments

If a function is defined in an assignment (f = function...end function), its outer is the current locals. If a function is defined in a statement call (print function...end function) or after a return, its outer is globals.

Does it need to be fixed or described as a feature in the docs?

x = "bye"

f = function(x)
	ff = function
		print outer.x
	end function
	ff
end function
f "hi"      // hi

f2 = function(x)
	m = {"ff": "ff"}
	m.replace "ff", function
		print outer.x
	end function
	m.ff
end function
f2 "hi"     // bye

f3 = function(x)
	return function
		print outer.x
	end function
end function
a = f3("hi")
a           // bye

marcgurevitx avatar Nov 21 '23 19:11 marcgurevitx

Umm, I remember we had drawn the conclusion before that the outer space of an anonymous function is always globals, wherever it is defined. https://discord.com/channels/646000428441534474/646000428441534478/1165859154590699541

Withered-Flower-0422 avatar Nov 21 '23 19:11 Withered-Flower-0422

That's right. But this might naturally change when we change how functions are parsed (#100). So, let's leave this ticket open to remind us to consider this behavior carefully.

JoeStrout avatar Nov 21 '23 20:11 JoeStrout