Do or Die

Share your advanced PureBasic knowledge/code with the community.
User avatar
GedB
Addict
Addict
Posts: 1313
Joined: Fri May 16, 2003 3:47 pm
Location: England
Contact:

Do or Die

Post 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.
Last edited by GedB on Thu Mar 18, 2004 4:47 pm, edited 1 time in total.
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post 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! :)
--Kale

Image
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Very interesting!

Thanks.
Fred
Administrator
Administrator
Posts: 18252
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

It's pure luck than it works ! :)
ricardo
Addict
Addict
Posts: 2438
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

Post 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.
Last edited by ricardo on Thu Mar 18, 2004 8:28 pm, edited 1 time in total.
ARGENTINA WORLD CHAMPION
User avatar
GedB
Addict
Addict
Posts: 1313
Joined: Fri May 16, 2003 3:47 pm
Location: England
Contact:

Post by GedB »

Fred,

Are we safe using it, or is this behaviour likely to be 'fixed' in a later release?
User avatar
GedB
Addict
Addict
Posts: 1313
Joined: Fri May 16, 2003 3:47 pm
Location: England
Contact:

Post 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.
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post 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! :)
--Kale

Image
ricardo
Addict
Addict
Posts: 2438
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

Post 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.
ARGENTINA WORLD CHAMPION
ricardo
Addict
Addict
Posts: 2438
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

Post 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:
ARGENTINA WORLD CHAMPION
freak
PureBasic Team
PureBasic Team
Posts: 5944
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post 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
quidquid Latine dictum sit altum videtur
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post 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)
Last edited by Psychophanta on Fri Mar 19, 2004 12:35 pm, edited 1 time in total.
Fred
Administrator
Administrator
Posts: 18252
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

This is not safe, I suggest to always put an 'IF' before, as an expression requiers a keyword.
User avatar
GedB
Addict
Addict
Posts: 1313
Joined: Fri May 16, 2003 3:47 pm
Location: England
Contact:

Post 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?
User avatar
Rings
Moderator
Moderator
Posts: 1435
Joined: Sat Apr 26, 2003 1:11 am

Post by Rings »

a Try-Catch construct is here, but that is not what you mean......
SPAMINATOR NR.1
Post Reply