Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I recover from a failed import in Nim?
    text
    copied!<p>In <a href="http://nim-lang.org" rel="nofollow">Nim</a> I can write the following code to import an external module:</p> <pre><code>import myFancyPantsModule ... # And here I'd use the fancyPants proc </code></pre> <p>This works fine as long as I have the module, but for people who might download the code and not have the module installed compilation will fail with a not very user friendly message:</p> <pre><code>$ nim c fancyProgram.nim fancyProgram.nim(1, 7) Error: cannot open 'myFancyPantsModule' </code></pre> <p>Is there any way I can wrap around the <code>import</code> so that I can <em>catch</em> it similar to an exception and execute an alternative branch of code similar to the <code>when</code> statement? I was hoping to find some <code>importable</code>-like macro or something which I could use like:</p> <pre><code>when importable(myFancyPantsModule): # And here I'd use the fancyPants proc else: quit("Oh, sorry, go to https://github.com/nim-lang/nimble and install " &amp; " the myFancyPantsModule using the nimble package manager") </code></pre> <p>In fact, rather than a simple error message I would like to make some modules optional, so that compilation still proceeds ahead, maybe with reduced functionality. Is this possible?</p> <p><strong>SOLUTION EDIT</strong>: Based on the answer here is my version how to solve the issue, first you need a <code>moduleChecker</code> binary with the following source:</p> <pre><code>import os, osproc let tmpFile = getTempDir() / "dynamicModuleChecker.nim" proc checkModule(module: string) = except: echo "Cannot write ", tmpFile, " to check the availability of modules" quit(1) writeFile(tmpFile, "import " &amp; module &amp; "\n") finally: removeFile(tmpFile) except: echo("Cannot run \"nimrod check\" to check the availability of modules") quit(1) if execCmdEx("nim check " &amp; tmpFile).exitCode != 0: echo("Cannot import module " &amp; module &amp; ".") quit(1) else: echo "OK" if ParamCount() &lt; 1: quit("Pass as first parameter the module to check") else: checkModule(ParamStr(1)) </code></pre> <p>Then, having this command available the following macro can be used:</p> <pre><code>import macros macro safeImport(module, message: string): stmt = if "OK" == gorge("./moduleChecker " &amp; module.strVal): result = newNimNode(nnkStmtList).add( newNimNode(nnkImportStmt).add( newIdentNode(module.strVal))) else: error("\nModule " &amp; module.strVal &amp; " not available.\n" &amp; message.strVal) safeImport("genieos", "Please install \"http://gradha.github.io/genieos/\"") </code></pre> <p>It is too unfortunate that a separate process has to be spawned, not only for the external compilation but also another one to generate the temporary file to check, as there is no <code>staticWrite</code> in the current version to generate files at compile time.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload