Page 1 of 2

Do or Die

Posted: Thu Mar 18, 2004 4:07 pm
by GedB
Code updated for 5.20+

I notice that the Or operator is shortcutted.

That is, if the first condition is True then the second condition is not evaluated.

This allows the use of the 'Do or Die' idiom common in Perl and PHP.

That is instead of writing

Code: Select all

Procedure Die(Message.s)
  MessageRequester("Fatal Error", Message)
  End
EndProcedure

Enumeration ;Windows
  #MainWindow
EndEnumeration

#MainWindowFlags = #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_ScreenCentered
If OpenWindow(#MainWindow,0,0, 500, 500, #MainWindowFlags, "Window") = 0
    Die("Unable to Open Main Window")
EndIf
  
Repeat
  Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow
You can write

Code: Select all

Procedure Die(Message.s)
  MessageRequester("Fatal Error", Message)
  End
EndProcedure

Enumeration ;Windows
  #MainWindow
EndEnumeration

#MainWindowFlags = #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_ScreenCentered
Bool(OpenWindow(#MainWindow,0,0, 500, 500, "Window", #MainWindowFlags) Or Die("Unable to Open Main Window"))
 
Repeat
  Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow
Considering the testing needed when creating the window, gadget list and other resources this seems like a handy little shortcut that tidies up the code nicely.

Posted: Thu Mar 18, 2004 4:15 pm
by Kale
Very interesting! It's kind of like the error handling i do using this code posted here by me:
viewtopic.php?t=9860
I like the elegance of your Die() function, if no-one comes up with any arguments for not using it i think i will start using your method! :)

Posted: Thu Mar 18, 2004 4:29 pm
by Dare2
Very interesting!

Thanks.

Posted: Thu Mar 18, 2004 5:37 pm
by Fred
It's pure luck than it works ! :)

Posted: Thu Mar 18, 2004 5:53 pm
by ricardo
Good idea!!!

Make a simple text and it works

Code: Select all

Procedure Die(msg.s)
    MessageRequester("",msg)
   ProcedureReturn 0 ;<<< This line must be
EndProcedure


If ReadFile(0,"non-exixtent-file.txt") Or Die("cant open the file!")
    CloseFile(0)
EndIf

;This second file exists
If ReadFile(0,"abc.txt") Or Die("cant open the file!")
    MessageRequester("",ReadString())
    CloseFile(0)
EndIf
[added]

I see that i should add a procedurereturn <> 1 to avoid that the procuderereturn is true.

Posted: Thu Mar 18, 2004 5:55 pm
by GedB
Fred,

Are we safe using it, or is this behaviour likely to be 'fixed' in a later release?

Posted: Thu Mar 18, 2004 5:57 pm
by GedB
Ricardo,

Good idea. In that case I would call the function Fail. Do or Fail.

This will distinguish it from Die, which actually halts execution.

Posted: Thu Mar 18, 2004 7:25 pm
by Kale
It's pure luck than it works !
Are we safe using it, or is this behaviour likely to be 'fixed' in a later release?
Lol! I hope this will be left as is, because it seems to make sense! :)

Posted: Thu Mar 18, 2004 8:30 pm
by ricardo
Fred wrote:It's pure luck than it works ! :)
I don't think so.

Correct me if im wrong, but with the OR the right hand part only is parsed IF the left one is not true.

Then, if you put one at the right (must response false too!) you can see when all at the left side fails.

Posted: Thu Mar 18, 2004 8:32 pm
by ricardo
GedB wrote:Ricardo,

Good idea. In that case I would call the function Fail. Do or Fail.

This will distinguish it from Die, which actually halts execution.
Yes, you are right! :wink:

Posted: Thu Mar 18, 2004 8:33 pm
by freak
Yes, the fact, that the 'Or' part is executed only if the first part is false, is intended.
But the fact, that it all works, even without an 'If' in front is pure luck.

Timo

Posted: Fri Mar 19, 2004 11:56 am
by Psychophanta
I think this trick is very interesting.
But Fred wrote:
It's pure luck than it works !
What do you mean?
Freak wrote:
without an 'If' in front is pure luck
Understand this is ambiguous: Does "pure luck" mean it is a dangerous trick? or "pure luck" simply means that in PB is intended to do that using If...EndIf and the trick is a very lucky casual but working discovery?
In other words: is this trick safe?
:?:

Why this is a Syntax Error?

Code: Select all

( InitSprite() And InitKeyboard() And InitMouse() ) Or MessageRequester("Error","Can't open DirectX",0)

Posted: Fri Mar 19, 2004 12:15 pm
by Fred
This is not safe, I suggest to always put an 'IF' before, as an expression requiers a keyword.

Posted: Fri Mar 19, 2004 12:43 pm
by GedB
Why this is a Syntax Error?
Since you have put in brackets it is an expression. The parser is catching expressions.

I think that OpenWindow() Or Die() is working because OpenWindow() on its own is allowable as a command.

It is then working with the Or becaue it is also an expression as a function.

The Parser is letting it through, and by sheer luck the ASM makes sense.

@Fred:

I do like using Do or Die, is there any way of keeping it?

Perhaps OrDie() and OrFail() keywords could be added and integrated with the OnError library?

Posted: Fri Mar 19, 2004 1:15 pm
by Rings
a Try-Catch construct is here, but that is not what you mean......