minxomat

"Or Die" in AutoIt

2 posts in this topic

This is a continuation of my "series" of how to introduce some new machinery into AutoIt through syntax shenanigans. The first one was Stack-based LINQing. And like the first one, this also is purely for entertainment purposes.

Some languages have a syntax construct "die", providing a quick way to make a somewhat graceful exit when something horrible happens (also, in PHP, you should die() at the end of every major execution path). There are two elements to a statement with die():

  • Check if something has gone wrong, and
  • die() if it has.
  • (Optional: Make horrible puns about dying)

I decided to abuse the logical, short-circuit (this is important, non-short-circuit languages like VB.NET have extra keywords for this) "Or". To enable statements as expressions, we need to disable Au3Check.

Here's a basic example:

#AutoIt3Wrapper_Run_AU3Check=n

$sTest = "hllo"

"hllo"  == $sTest Or Die("Oops") ; Works
"hallo" == $sTest Or Die("Oops") ; Dies

Func Die($s)
    Exit ConsoleWrite("!> " & $s & @LF)
EndFunc

Notice how there is no left side to the expression. If you provide a left side (e.g. a variable assignment), you can leave Au3Check on.

A more elaborate example:

#AutoIt3Wrapper_Run_AU3Check=n

(SomethingImportant() Or Die(ErrGUI)) ; Works
(SomethingImportant() Or Die(ErrCLI)) ; Dies

Func SomethingImportant()
    Static $bFail
    $bFail = Not $bFail
    Return $bFail
EndFunc

Func Die($f = ErrCLI)
    Exit $f("Error while doing something important.")
EndFunc

Func ErrGUI($s)
    MsgBox(0, "Error", $s)
EndFunc

Func ErrCLI($s)
    ConsoleWrite("!> " & $s & @LF)
EndFunc

Notice the extra parens. Those are needed because the "()" after "SomethingImportant" close the expression and cause an error.

As always, have fun :) 


I will answer every single PM, and you are free to ask anything anytime.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I like the simple, and easy to understand!

If (Not SomethingImportant()) Or (Not ImportantSomething()) Then Die(@error)
MsgBox(32, "", "I'm VIP") ; Works

Func SomethingImportant()
    Local $JON = "Handsome"
    Local $JOS = "Smart"
    If $JON <> $JOS Then Return True
    Return SetError(1, 0, False)
EndFunc   ;==>SomethingImportant


Func ImportantSomething()
    If Int("JOS hated me!") Then Return SetError(1, 0, False)
    Return True
EndFunc   ;==>ImportantSomething

Func Die($f = "!")
    Exit ConsoleWrite("Error while doing something important: " & $f)
EndFunc   ;==>Die

The complex will be confusing!

Edited by VIP
I ll not DIE (:

Regards,
 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now