sancarn / stdVBA

VBA Standard Library - A Collection of libraries to form a common standard layer for modern VBA applications.
MIT License
294 stars 58 forks source link

`stdLambda` Syntax Improvements #55

Open sancarn opened 2 years ago

sancarn commented 2 years ago

Tasks


Allow parameter names: stdLambda.Create("(a,b,c) => a*b+c")

Not only has this been heavily requested, it would improve ease of use and it isn't too tricky to implement.

Allow vbLet | vbSet: set a.b = something, let a.c = true

One of the downfalls of lambdas so far is the inability to trigger vbLet/vbSet There are 2 options here:

Have a let operator e.g. :=

a.b := something

This is probably the easiest to implement:

if consume() = eObjectGetter and peak(2) = eObjectSetter then
  beginLetExpression()
else
  beginGetExpression()
end if

Introduce a let an set keyword:

Not entirely sure how this would work internally but this would be the typical VBA approach.

set a.b = something
let a.c = 1

Allow column syntax [Some column] and include row property

Say you are looping through rows of a csv, it would be nice if we had a good way of accessing the columns of each row. The current easiest way to do this is with dictionaries:

stdLambda.Create("$1.item(""column 1"") + $1.col2").bind(row)

'or with change 1:
stdLambda.Create("(row)=>row.col1 + row.col2").bind(row)

This change proposes the addition of a row variable on stdLambda instances.

Dim lambda as stdLambda: set lambda = stdLambda.Create("[column 1] + [col2]")
for each row in csv
  set lambda.row = row
  if lambda() > 5 then
    '...
  end if
next

Add try-catch

Say we are using stdWindow to search for ProcessIDs e.g.

set eOut = wnds.Filter(stdLambda.Create("$2.ProcessID = $1").bind(proc.id))

stdWindow is built in such a way that if the window no longer exists ProcessID will throw an error. This can be worked around with if statements to catch the error:

set eOut = wnds.Filter(stdLambda.Create("if $2.exists then $2.ProcessID = $1 else false").bind(proc.id))

but it would be nice to have a try catch also:

set eOut = wnds.Filter(stdLambda.Create("Try $2.ProcessID = $1 Catch false").bind(proc.id))

Add short circuiting out of OR and AND