3
$\begingroup$

How to exit a package in the middle, for example,

BeginPackage["myPackage"]
Begin["`Private"]

DoSomething

If[$FrontEnd===Null, Abort[]]

DoSomethingIfFrontEndDefined

End[]
EndPackage[]

Here Abort[] doesn't really quit the whole package. Is there any command which can do this?

I could use Goto but that sounds not good. Also I don't want to Quit[] because I want to use the definitions in the DoSomething part.

$\endgroup$
1
  • 1
    $\begingroup$ What's wrong with If[$FrontEnd=!=Null,DoSomethingIfFrontEndDefined] ? And you are missing some trailing backquotes in your context names... $\endgroup$ Jan 18, 2014 at 23:09

1 Answer 1

5
$\begingroup$

Rather than $FrontEnd === Null, a better way to test for the existence of a front end is $Notebooks which is a boolean variable:

Kernel:

enter image description here

FrontEnd:

enter image description here

Coming to the crux of your question, your usage of If is a bit confusing... why are you not using the "else" branch? If you set up the conditions right, you can just place all your code in the "then" branch and leave it at that (assuming you don't want to make definitions if there's no front end):

BeginPackage["myPackage`"]
Begin["`Private"]

(* Do something *)
If[$Notebooks, (* Do something else if FE present *)]

(* Do something more *) 

End[]
EndPackage[]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.