Fat arrow =>

Discuss the future of the AutoHotkey language
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Fat arrow =>

26 Mar 2018, 06:53

Hello, very nice feature :bravo:.

I get error: f := () => u := 1 (due to assignment), I can do u++ and (u := 1) though.

I make unspecfic title if others have general comments or questions, we do not need many threads.
Although I like the name. fat arrow, I'd prefer something easier to type, eg f := () :: expr, fat colon? :lol: (Sorry if I'm the kid complaining about getting a blue ferrari instead of a red)
I do not know how you type => on english keyboard, but on mine, I do shift+0 for =, then shift + < for >.


Cheers.
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Fat arrow =>

26 Mar 2018, 09:26

Hi, I have come across a *.js file which looks promising for implementing a testing facility.
Unfortunately, it relies on a similar notation which is new to me. Can anybody please tell me if AHK's new FatArrow notation is similar enough to convert this JavaScript:

Code: Select all

function check(current,other){
	if(typeof other !== 'object' && typeof current !== 'object') return current === other;
	let equal = false;
	for(let prop in other){
		if(current[prop] === undefined) throw new Error("FAILED");
		equal = equal || check(current[prop],other[prop]);
	}
	if(!equal) throw new Error("FAILED");
	return equal;
}


function test(comment,callback){
	let error;
	try{
		//execute the tests;
		callback();
	}catch(e){
		// Get the result whether it passed or failed 
		error = e.message;
	}
	console.log(comment,error === "FAILED" ? "FAILED" : "PASSED");
}

function expect(value){
	this.toBe = function(other){
		if(value !== other)	throw new Error("FAILED");
	}
	this.toEqual = function(something){
		check(value,something);
	}
	return this;
}

// The tests start HERE

test('Is 2+2 be 4?',() => {
	expect(2 + 2).toBe(4);						// returns : Is 2+2 be 4? PASSED
});

test('Is 2+3 be 4?',() => {
	expect(2 + 3).toBe(4);						// returns : Is 2+3 be 4? FAILED
});

test('Is {value:3*3} equal {value:9}?',() => {
	expect({value:3*3}).toEqual({value:9});        			// returns : Is {value:3*3} equal {value:9}? PASSED
});

test('Is {value:2*3} equal {value:9}?',() => {
	expect({value:2*3}).toEqual({value:9});       			// returns : Is {value:2*3} equal {value:9}? FAILED
});

test('Is {value:{arr:[6]}} equal {value:9}?',() => {
	expect({value:{arr:[6]}}).toEqual({value:9});    		// Is {value:{arr:[6]}} equal {value:9}? FAILED
});
User avatar
Flipeador
Posts: 1204
Joined: 15 Nov 2014, 21:31
Location: Argentina
Contact:

Re: Fat arrow =>

26 Mar 2018, 10:53

I like it too! It looks great :D
I like the new operator. This is a lambda expression, right?
I write => just like you. I do not see anything bad in "not so simple to write", since it is not in common use, I would say.
Image

btw, fat arrow sounds very strange for me :lol:
offtopic: now, within the most significant, and as I mentioned in another topic, it would be nice to have VarSetLength (and the equivalent for objects) in the next update.
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Fat arrow =>

26 Mar 2018, 11:41

Did we really get lambda expressions?
Maybe it's time to move to AHK v2
Recommends AHK Studio
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Fat arrow =>

26 Mar 2018, 11:49

@ wolf_II, I do not know, but hi :wave:.
@ Flipeador, good point about automating it, cheers.
@ nnnik, :angel: :arrow: v2

Cheers.
User avatar
Flipeador
Posts: 1204
Joined: 15 Nov 2014, 21:31
Location: Argentina
Contact:

Re: Fat arrow =>

26 Mar 2018, 15:22

Code: Select all

EnumWindows(ParentID := 0, Visible := FALSE)
{
    Local pEnumChildProc := CallbackCreate("EnumChildProc", "F&", 2)    ; You can't do it Static. is it a bug?
        , oWindows       := []

    Local R := DllCall("User32.dll\EnumChildWindows", "Ptr", ParentID, "UPtr", pEnumChildProc, "UPtr", 0)
    CallbackFree(pEnumChildProc)
    Return R ? oWindows : FALSE


    ; ==================================================================
    ; Nested Functions
    ; ==================================================================
    EnumChildProc(params)    ; EnumChildProc(HWND hwnd, LPARAM lParam)
    {
        ; You must specify oWindows and Visible with lambda, or else, it will not work. is it a bug?
        Return ((WindowId,oWindows,Visible) => !Visible || DllCall('User32.dll\IsWindowVisible', 'Ptr', WindowId) ? oWindows.Push(WindowId) : 1).call(NumGet(params, "Ptr"), oWindows, Visible)
        ;Local WindowId := NumGet(params, "Ptr")
        ;If (!Visible || DllCall('User32.dll\IsWindowVisible', 'Ptr', WindowId))
        ;oWindows.Push(WindowId)
        ;Return 1
    } ; https://msdn.microsoft.com/en-us/library/windows/desktop/ms633493(v=vs.85).aspx
} ; https://msdn.microsoft.com/en-us/library/windows/desktop/ms633494(v=vs.85).aspx





For Each, WindowId in EnumWindows(0, TRUE)
    List .= "[" . WindowId . "] " . WinGetClass("ahk_id" . WindowId) . "`n"
MsgBox List
User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: Fat arrow =>

27 Mar 2018, 16:02

Whoa! This is huge!
User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: Fat arrow =>

27 Mar 2018, 20:34

It doesn't seem like these can be multiline as in JavaScript. Anyone else able to get one to work on more than one line?
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: Fat arrow =>

27 Mar 2018, 20:45

kczx3 wrote:It doesn't seem like these can be multiline as in JavaScript. Anyone else able to get one to work on more than one line?
i would think it would work with how you would normally split things into multiple lines
https://lexikos.github.io/v2/docs/Scrip ... ntinuation

this works:

Code: Select all

EnumWindows(ParentID := 0, Visible := FALSE)
{
    Local pEnumChildProc := CallbackCreate("EnumChildProc", "F&", 2)    ; You can't do it Static. is it a bug?
        , oWindows       := []

    Local R := DllCall("User32.dll\EnumChildWindows", "Ptr", ParentID, "UPtr", pEnumChildProc, "UPtr", 0)
    CallbackFree(pEnumChildProc)
    Return R ? oWindows : FALSE

    ; ==================================================================
    ; Nested Functions
    ; ==================================================================
    EnumChildProc(params)    ; EnumChildProc(HWND hwnd, LPARAM lParam)
    {
        Return ((WindowId
                ,oWindows
                ,Visible) => !Visible || DllCall('User32.dll\IsWindowVisible', 'Ptr', WindowId) 
                                               ? oWindows.Push(WindowId) 
                                               : 1).call(NumGet(params, "Ptr")
                                                       , oWindows
                                                       , Visible)
    }
}


For Each, WindowId in EnumWindows(0, TRUE)
    List .= "[" . WindowId . "] " . WinGetClass("ahk_id" . WindowId) . "`n"
MsgBox List

Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Fat arrow =>

28 Mar 2018, 01:02

Mulitiline seems to work as expected.

Code: Select all

(...) 
=> expr
where (...) and expr can be multilined as expected. Any example which didn't work as you expected kczx3? Disregarding how javascript works.

Cheers.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Fat arrow =>

28 Mar 2018, 01:33

Flipeador wrote:; You can't do it Static. is it a bug?
Since EnumChildProc is a closure, I'm not sure there is a well defined scope when the static initialising takes place, even if it did work I'm not sure what to expect :think:. However, it seems you also cannot do a static initialisation to nested functions, that seems more bug like,

Code: Select all

f(){
	static a := callbackcreate("g") ; error
	g(){
	}
}
But passing func("g") above works as expected. But not

Code: Select all

static n := "g"
static a := callbackcreate(func(n)) ; error
unless I define a function g outside of f.
; You must specify oWindows and Visible with lambda, or else, it will not work. is it a bug?
I'm not sure what you mean.

Cheers. :wave:
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Fat arrow =>

28 Mar 2018, 02:12

Yeah I expected the fat arrow expression ( lambda expression ) to be able to handle multiple seperate expression lines.
I don't see how the current thing could be particulary useful.
Recommends AHK Studio
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Fat arrow =>

28 Mar 2018, 02:16

What do you mean nnnik? I saw no problems with multiline expressions. Do you have an example not working properly?
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Fat arrow =>

28 Mar 2018, 02:18

Seperate expressions. Using , at a new line is just a sucky method of combining it into a single expression.
Recommends AHK Studio
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Fat arrow =>

28 Mar 2018, 02:20

e. g.

Code: Select all

( p* ) => { DllCall(1)
DllCall(2) }
Recommends AHK Studio
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Fat arrow =>

28 Mar 2018, 02:22

I see, yes that would be nice ofc.
User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: Fat arrow =>

28 Mar 2018, 04:07

I was talking about what nnnik references regarding multiple expressions
User avatar
Flipeador
Posts: 1204
Joined: 15 Nov 2014, 21:31
Location: Argentina
Contact:

Re: Fat arrow =>

28 Mar 2018, 04:28

Helgef wrote:
Flipeador wrote:; You must specify oWindows and Visible with lambda, or else, it will not work. is it a bug?
I'm not sure what you mean.
I mean, in the lambda scope, oWindows and Visible variables are not defined; so you must pass them through parameters.
You cannot do:

Code: Select all

Return (WindowId => !Visible || DllCall('User32.dll\IsWindowVisible', 'Ptr', WindowId) ? oWindows.Push(WindowId) : 1).call(NumGet(params, "Ptr"))
Maybe I missed something in the documentation :lol:
Helgef wrote:Since EnumChildProc is a closure
I must pay more attention :wave:
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Fat arrow =>

28 Mar 2018, 07:40

using the recent update with automatic continuation inside {} and [] allows for:

Code: Select all

() => [
	DllCall( 1 )
	DllCall( 2 )
	DllCall( 2 )
]
and

Code: Select all

() => {
	lambda:
	DllCall( 1 )
	DllCall( 2 )
}
Still no If/Else throw Loops or break.
Recommends AHK Studio
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Fat arrow =>

28 Mar 2018, 07:42

You cannot do:
I cannot deduce from the documentation that that would be expected, reducing your example,

Code: Select all

f()
f(){
	a := 1
	g()
	msgbox a	; blank - unexpected
	g(){
		() => a
	}
}
but it is not related to the fat arrow, you get the same result when replacing it with

Code: Select all

h(){
	(a)
}
in g.
As a side note, @Flipeador, you could make your function like this instead,

Code: Select all

EnumWindows(ParentID := 0, Visible := FALSE)
{
    static EnumChildProc := (oWindows, Visible, WindowId) => !Visible || DllCall('User32.dll\IsWindowVisible', 'Ptr', WindowId := NumGet(WindowId, "Ptr")) ? oWindows.Push(WindowId) : 1
	
	Local oWindows := []
	Local pEnumChildProc := CallbackCreate(EnumChildProc.bind(oWindows, Visible), "F&", 2)
  
    
    Local R := DllCall("User32.dll\EnumChildWindows", "Ptr", ParentID, "UPtr", pEnumChildProc, "UPtr", 0)
    CallbackFree(pEnumChildProc)
    Return R ? oWindows : FALSE
} ; https://msdn.microsoft.com/en-us/library/windows/desktop/ms633494(v=vs.85).aspx


Return to “AutoHotkey Development”

Who is online

Users browsing this forum: No registered users and 26 guests