Note that there are some explanatory texts on larger screens.

plurals
  1. POMultiple Exits From F# Function
    text
    copied!<p>I could do this easily in C++ (note: I didn't test this for correctness--it's only to illustrate what I'm trying to do):</p> <pre><code> const int BadParam = -1; const int Success = 0; int MyFunc(int param) { if(param &lt; 0) { return BadParam; } //normal processing return Success; } </code></pre> <p>But I cannot figure out how to exit a routine early in F#. What I want to do is to exit the function on a bad input but continue if the input is ok. Am I missing some fundamental property of F# or am I approaching the problem in the wrong way since I'm just learning FP? Is a <em>failwith</em> my only option here?</p> <p>This is what I've got so far and it compiles ok:</p> <pre><code> #light module test1 (* Define how many arguments we're expecting *) let maxArgs = 2;; (* The indices of the various arguments on the command line *) type ProgArguments = | SearchString = 0 | FileSpec = 1;; (* Various errorlevels which the app can return and what they indicate *) type ProgReturn = | Success = 0 | WrongNumberOfArgumentsPassed = 1;; [&lt;EntryPoint&gt;] let main (args:string[]) = printfn "args.Length is %d" args.Length let ProgExitCode = if args.Length &lt;&gt; maxArgs then printfn "Two arguments must be passed" int ProgReturn.WrongNumberOfArgumentsPassed (* Want to exit "main" here but how? *) else int ProgReturn.Success let searchstring, filespec = args.[int ProgArguments.SearchString],args.[int ProgArguments.FileSpec]; printfn "searchstring is %s" searchstring printfn "filespec is %s" filespec ProgExitCode;; </code></pre> <p>Is there an FP way of dealing with this sort of thing? </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