https://lexikos.github.io/v2/docs/Varia ... #fat-arrow () => expr it is simple sintax but let see what it can do.
Examples:
Lets begin with something simple.
Code: Select all
sup:= 7
function()
function() {
global ;global scope to function()
out:= 10
;function name is visible inside whole containing body
scop( 5)
;scop()'s variables scope is the same as scope containing scop()
scop(arg) => (msgBox("arg= " arg " out= " out " sup= " sup))
}
The most important things in one alghoritm is divergency and cyclicality.
Let see what about if/else/loop/for/do/while/until.
Code: Select all
glo:= 7
obj:= {one:"oneVal", two:"twoVal"}
function()
function() {
global
extr()
extr() => (
(if glo == 13 ;no action
MsgBox("true " (glo==13))
),
(loop 10 ;no action
MsgBox("loop A_Index= " A_Index)
),
(while glo > 0 ;no action
MsgBox("while A_Index= " A_Index)
),
(for k,v in obj ;no action
MsgBox("key:val= [" k ":" v "]")
) )
}
About IF.
Code: Select all
function()
function() { ;ternary instead if/else
_if(5)
_if(arg) => (arg == 5? MsgBox(arg "==5"): ;if a==5 then MsgBox(arg "==5")
MsgBox(arg "!=5") ; else MsgBox(arg "!=5")
) ;"" must be used as nothing
;_if(arg) => (arg == 5? MsgBox(arg): "") ; at both places (then/else)
}
Code: Select all
function()
function() { ;recursion instead loop/while
_loop(10)
_loop(arg) => (
arg > 0 ? tooltip(arg) sleep(1000) _loop(arg-1) ;no comma
: tooltip()
)
}
Different expressions not enclosed and not comma separated it is possible but not well.
Yet another recursion example.
Code: Select all
function()
function() {
;global
local sta ;_for use local variable
;to keep value in recursion
_for()
_for() => ( ;for(sta:=10; sta > 2; sta:=sta-1) {
; tooltip("sta= " sta)
; sleep(mis)
;}
;tooltip()
(sta:= sta? sta-1: 10),
(sta > 2 ? tooltip("sta= " sta) sleep(1000) _for()
: tooltip()
) )
}
Code: Select all
function()
MsgBox("global= " glo)
function() {
global
out:= 10
recu()
recu() => (
(glo:= glo? glo: 20), ;create global variable (if global)
;comma is a must
(out > 0? sub_%mod(out, 2)%() ;new line
sleep(1000) out-- recu() ;no comma
: ""
),
;fat arrow function in fat arrow function
sub_1() => (tooltip("1= " out " glo= " (glo-=1)))
)
sub_0() => (tooltip("0= " out " glo= " (glo-=1)))
}
Code: Select all
;think for fat arrow function as expresion that must return result
FatArrowFunc([byRef]arg) => (
;variables
; byRef - work as usual
; scope - the same as scope of containing scope
; create variables - as part of expression
; global ;no affect even persist
; local ;"
; static ;"
; - newly created variable can be
; local for fatArrowFunc()
; or
; global (if global persyst in containing scope)
; - there no mechanism subfunction to create
; variable into containing function
(<var:= expr/func>), ;comma is a must if new variable created
;() and , is not mandatory but using them
; is best for syntax and readability
(<expr/func>) ;space or new line separate expressions
;using () and comma is the best and clear syntax
;if/else/loop/for/do/while/until
; and so on not expressions not ussable
((arg)? (<expr/func>): (<expr/func>))
;best syntax - brakets and commas
;FatArrowFunc(arg) => (
; (<expression>),
; (<expression>),
; ((<expression>) ? (<expression>)
; : (<expression>)
; ),
; "" ;do nothing (NOP)
;)
;retun ;forbiden
; ;think that return is at => place
; =>
;FatArrowFunc(arg) { return (
; (<expr>),
; (<expr>)
;)
;}
;last expression is return value
)
Code: Select all
#SingleInstance Force
;Copyright (c) D.Donchev
mis:= 200
sup:= 7
obj:= {one:"oneVal", two:"twoVal"}
function()
msgBox("glo= " glo) ;see recu()
function() {
global ;function() scope to global
out:= 10
;scop example ==========
scop( 5) ;function name is visible
;inside whole containing body
scop(arg) => ( ;scop() variable scope is the same as
;scope containing scop()
msgBox("arg= " arg " out= " out " sup= " sup)
)
;}
;cycl example ==========
cycl(10) ;recursion
cycl(arg) => ( ;recursion instead loop
arg > 0 ? tooltip(arg) sleep(mis) cycl(arg-1) ;no comma
: tooltip()
)
;}
;recu example ==========
recu() ;visibility
recu() => (
(glo:= glo? glo: 20), ;create global variable (if global)
;comma is a must
(out > 0? sub%mod(out, 2)%() ;new line
sleep(mis) out-- recu() ;no comma
: ""
), ;enclosure ternary expression
;fat arrow function in fat arrow function
sub1() => (tooltip("1= " out " glo= " (glo-=1)))
)
sub0() => (tooltip("0= " out " glo= " (glo-=1)))
;}
;_for example ==========
local sta ;_for use local variable
;to keep value in recursion
_for()
_for() => ( ;for(sta:=10; sta > 2; sta:=sta-1) {
; tooltip("sta= " sta)
; sleep(mis)
;}
;tooltip()
(sta:= sta? sta-1: 10),
(sta > 2 ? tooltip("sta= " sta) sleep(mis) _for()
: tooltip()
) )
;}
;extr example ==========
extr()
extr() => (
(if glo == 13 ;no action
MsgBox("true " (glo==13))
),
(loop 10 ;no action
MsgBox("loop A_Index= " A_Index)
),
(while sup > 0 ;no action
MsgBox("while A_Index= " A_Index)
),
(for k,v in obj ;no action
MsgBox("key:val= [" k ":" v "]")
) )
}
ExitApp
#!Q::ExitApp
Code: Select all
myToolTip("111", 200, 200, 20, 3000)
myToolTip("222", 100, 100, , 2000)
myToolTip(text, x:="", y:="", w:="", time:="") {
tooltip(text, x, y, w)
if(time is "number")
SetTimer(()=>tooltip(,,,w), time<0?time:-time) ;fat as argument
}
Code: Select all
class A {
static var:= 1
met() {
return 2
}
fat()=>(3)
}
for k,v in A
MsgBox(k " : " v " -> " type(v))
MsgBox("outside A => " fat()) ;fat() is global becouse class definition is global
Code: Select all
_Class : A -> String (ok)
var : 1 -> Integer (ok)
met : -> Func (ok)
fat is invisible lol --> (no)
is not a member of A (ok)
Code: Select all
#SingleInstance Force
;Copyright (c) D.Donchev
class Def {
static ClassVar
one:= 1
two:= 2
;Example (1) run last set fat function
ini(arg:="") {
if(type(arg) == "Func")
this.ClassVar:= arg ;set
if(type(this.ClassVar) != "Func")
this.ClassVar:= ()=>"default" ;default
MsgBox(%this.ClassVar%()) ;action
}
;Example (2) run argument fat func or internal
tos(arg:="") {
;arg:= type(arg) == "Func"? arg: ()=>"no format"
if(type(arg) != "Func")
arg:= ()=>"no format"
return %arg%(this)
} }
Ins:= new Def
;Example (1) ----------
Def.ini() ;default
Ins.ini() ;default
;expect: if ClassVar not set then init
;OK
Def.ini(()=>"class new") ;class new
Ins.ini() ;class new
;expect: if class ClassVar is changed then instance ClassVar changed
;OK
Ins.ini(()=>"instanse new") ;instanse new
Def.ini() ;class new
Ins.ini() ;instance new
;expect: ;if instance ClassVar is changed then class ClassVar NOT changeg
;OK
;Examle (2) ----------
MsgBox(Ins.tos())
;expected: run internal default fat func
;OK
MsgBox(Ins.tos((this) => ("myown= " this.one " then " this.two)))
;expected: run argument fat func
;OK
MsgBox(Ins.tos((this) => ( ;recursion demo
;for(k, v in this)
; res.= k " : " v " | "
;return res
;3 lines - without pushing arguments
;(_enum:= this._NewEnum()),
;_act()=>(_enum.Next(k,v)? ((res .= k ":" v "|"), _act()): res),
;_act()
;2 lines - stack breaker (LOL)
_act(_enum)=>(_enum.Next(k,v)? ((res .= k ":" v "|"), _act(_enum)): res),
_act(this._NewEnum())
)))
;expected: recursion must end
;OK
ExitApp
#!Q::ExitApp ;if something wrong
Again:
Enclose all individual expression in () and separate expressions with comas. Enclose all expressions in () like {} for best reading. "" can be used as nothing (NOP). Ternary instead IF/ELSE and recursion instead LOOP/WHILE.
Code: Select all
[FatArrowFunctionName]([agruments])=>(
(expression),
(expression) ;last expression is return value
)