Skip to content

Instantly share code, notes, and snippets.

@killerswan
Last active December 21, 2015 13:08
Show Gist options
  • Save killerswan/6310078 to your computer and use it in GitHub Desktop.
Save killerswan/6310078 to your computer and use it in GitHub Desktop.
Trouble toggling or not toggling a default function. (How do I do something like Python's `__name__ == "__main__"`?)
#if INTERACTIVE
let args = fsi.CommandLineArgs
if args.[0].Contains("Seq2.fsx") then
// works when run with fsharpi as one bash command
//
// Usage A:
// $ fsharpi Seq2.fsx
main (args.[1..])
else
// works when loaded and opened in fsi
//
// Usage B:
// $ fsharpi
// > #load "Seq2.fsx";;
// > open Seq2;;
printfn "« not running Seq2 main »"
0
#else
#if MAIN_SEQ2
// works when built with a custom symbol for these compiler directives
//
// Usage C:
// $ fsharpc --define:MAIN_SEQ2 --target:exe Seq2.fsx
// $ mono Seq2.exe
[<EntryPoint; STAThread>]
let demo args =
// although here we also have access
// to System.Environment.GetCommandLineArgs(),
// which also has item 0, too (unlike entrypoint args)
main args
#else
// Usage D:
// $ fsharpc --target:exe OtherThing.fsx
// $ mono OtherThing.exe
#endif
#endif
@killerswan
Copy link
Author

Well, I suppose I can define a symbol to test with compiler directives like MAIN_SEQ2 and get by, but there's gotta be a more elegant way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment