Defer

Discuss the future of the AutoHotkey language
iseahound
Posts: 355
Joined: 13 Aug 2016, 21:04
GitHub: iseahound

Defer

26 Oct 2018, 23:42

jeeswg wrote:
18 Oct 2018, 17:34
@iseahound: If higher level concepts have influenced your AutoHotkey v2 Development ideas, what about lower level concepts? Assembly language etc. Cheers.
Defer would allow functions to execute code after the function has returned. For reference this functionality already exists you can write return a, b:=a

With defer you can just write:

Code: Select all

function() {
Defer b := a
Return a
Its main use would be to free allocated memory after returning the contents of the memory.

https://tour.golang.org/flowcontrol/12
Helgef
Posts: 3303
Joined: 17 Jul 2016, 01:02
Contact:

Re: Defer

27 Oct 2018, 09:30

It can be useful :thumbup:.
For reference this functionality already exists you can write return a, b:=a
For reference, no, it is not available. In v1, it evaluates the sub-expressions from left to right, before the function returns at that specific return statement, and returns the leftmost result. In v2, (a, b:=a) is evaluated from left to right, but the expression returns the result from the rightmost sub-expression.

Cheers.

Edit, spoiler doesn't seem to work. Spoiler-tag:
Simple function example, having similar effect,

Code: Select all

f 123
f(a){
	b := defer( () => free(a) )
	return msgbox('return line')
	free(p){
		msgbox p
	}
}

defer(f){
	return { base: { __delete : f } }
}
iseahound
Posts: 355
Joined: 13 Aug 2016, 21:04
GitHub: iseahound

Re: Defer

28 Oct 2018, 13:56

Yes your code is exactly right. Defer resembles the __Delete meta function of an object. I've found myself creating objects with only __New and __Delete to replicate this functionality.

Currently in v1 return a, DllCall("GlobalFree", "ptr", a) returns a and not the result of the DllCall. (I'm writing the above code from memory, and I'm pretty sure a is just a pointer to the data. )

Return to “AutoHotkey v2 Development”

Who is online

Users browsing this forum: No registered users and 2 guests