Jump to content

"Or Die" in AutoIt

Recommended Posts


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:


$sTest = "hllo"

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

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

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:


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

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

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

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

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

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

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 (:


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