1
$\begingroup$

I have a bunch of definitions written in a file that I load with Get["..."], . I would like add a line into the the file so that the evaluation of these definitions goes ahaead only if some conditions are satisfied.

I know that one way of achieving this is wrapping the entire file in an "If", eg

If[ checks,
   my entire original file
]

but I have aesthetical concerns about this method. I was wondering if there was a way of achieving this in another way.

$\endgroup$
5
  • $\begingroup$ Why not just handle the condition at the point where you Get the file? If[ cond, Get["file.m"]] $\endgroup$ Commented May 22, 2023 at 7:40
  • $\begingroup$ If I send the definitions to someone else, then I would need to rely on them to adopt that good practice. $\endgroup$ Commented May 22, 2023 at 8:27
  • $\begingroup$ Use an If statement to set a flag in your definitions file, then wrap each definition in an If that checks the flag? $\endgroup$ Commented May 22, 2023 at 15:13
  • $\begingroup$ Instead of checking a condition to load your definitions, you can negate the condition and Abort if it's not satisfied, perhaps adding a Message(or Print) to inform the user why. I.e. add If[!condition], Message[...];Abort[]] at the top of your file and exit. This avoids the necessity of wrapping all your code. An alternative is to use two files, one containing your definitions, and another to load the definitions provided the condition is satisfied. So your users call Get[myFile.wl], which consists of If[conditions,Get[myFileDefinitions.wl]. $\endgroup$ Commented May 23, 2023 at 0:44
  • $\begingroup$ @jdp The first suggestion seems to achieve exactly what I wanted to do. $\endgroup$ Commented May 23, 2023 at 12:56

1 Answer 1

2
$\begingroup$

A more WL-like way of doing this, would be to write a function that makes the definitions and put a condition on that function. Something like:

MakeDefinitions[] /; cond := (
    (* current contents of file.m *)
    ...
);

You put that inside your file and always Get it no matter what. Then at the point where you need the definitions, you run MakeDefinitions[] instead of doing Get["file.m"]. The body of the function will only run if the definitions are fulfilled. As a bonus, you can now also turn MakeDefinitions into a proper function with arguments.

Edit

It seems like there's a mechanism to bail out of Get that you can use. Try putting this in a .m file and Getting it:

Print[1];

If[ RandomChoice[{True, False}], Return[Null, Get]];

Print[2];

I didn't realise before that the (undocumented) 2-argument Return can bail you out to the nearest Get, but it seems to work. The first argument is the return value for the Get, which can be anything you like but Null is a good default.

$\endgroup$
2
  • $\begingroup$ That still requires wrapping the entire contents in something, but it is a neat solution. $\endgroup$ Commented May 23, 2023 at 12:53
  • $\begingroup$ @Mercan Actually, I think I found another method that you might like. Let me update the answer $\endgroup$ Commented May 23, 2023 at 13:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.