Page 1 of 3
Fat arrow =>
Posted: 26 Mar 2018, 06:53
by Helgef
Hello, very nice feature
.
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?
(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.
Re: Fat arrow =>
Posted: 26 Mar 2018, 09:26
by wolf_II
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
});
Re: Fat arrow =>
Posted: 26 Mar 2018, 10:53
by Flipeador
I like it too! It looks great
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.
btw,
fat arrow sounds very strange for me
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.
Re: Fat arrow =>
Posted: 26 Mar 2018, 11:41
by nnnik
Did we really get lambda expressions?
Maybe it's time to move to AHK v2
Re: Fat arrow =>
Posted: 26 Mar 2018, 11:49
by Helgef
@
wolf_II, I do not know, but hi
.
@
Flipeador, good point about
automating it, cheers.
@
nnnik,
v2
Cheers.
Re: Fat arrow =>
Posted: 26 Mar 2018, 15:22
by Flipeador
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
Re: Fat arrow =>
Posted: 27 Mar 2018, 16:02
by kczx3
Whoa! This is huge!
Re: Fat arrow =>
Posted: 27 Mar 2018, 20:34
by kczx3
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?
Re: Fat arrow =>
Posted: 27 Mar 2018, 20:45
by guest3456
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
Re: Fat arrow =>
Posted: 28 Mar 2018, 01:02
by Helgef
Mulitiline seems to work as expected.
where
(...) and
expr can be
multilined as expected. Any example which didn't work as you expected
kczx3? Disregarding how javascript works.
Cheers.
Re: Fat arrow =>
Posted: 28 Mar 2018, 01:33
by Helgef
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
. 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.
Re: Fat arrow =>
Posted: 28 Mar 2018, 02:12
by nnnik
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.
Re: Fat arrow =>
Posted: 28 Mar 2018, 02:16
by Helgef
What do you mean nnnik? I saw no problems with multiline expressions. Do you have an example not working properly?
Re: Fat arrow =>
Posted: 28 Mar 2018, 02:18
by nnnik
Seperate expressions. Using , at a new line is just a sucky method of combining it into a single expression.
Re: Fat arrow =>
Posted: 28 Mar 2018, 02:20
by nnnik
e. g.
Code: Select all
( p* ) => { DllCall(1)
DllCall(2) }
Re: Fat arrow =>
Posted: 28 Mar 2018, 02:22
by Helgef
I see, yes that would be nice ofc.
Re: Fat arrow =>
Posted: 28 Mar 2018, 04:07
by kczx3
I was talking about what nnnik references regarding multiple expressions
Re: Fat arrow =>
Posted: 28 Mar 2018, 04:28
by Flipeador
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
Helgef wrote:Since EnumChildProc is a closure
I must pay more attention
Re: Fat arrow =>
Posted: 28 Mar 2018, 07:40
by nnnik
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.
Re: Fat arrow =>
Posted: 28 Mar 2018, 07:42
by Helgef
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
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