Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 29th, 2012  |  syntax: None  |  size: 0.80 KB  |  hits: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Is there a way to override a module's main function in the D programming language?
  2. #!/usr/bin/env rdmd -version=scriptedmain
  3.  
  4. module scriptedmain;
  5.  
  6. import std.stdio;
  7.  
  8. int meaningOfLife() {
  9.     return 42;
  10. }
  11.  
  12. version (scriptedmain) {
  13.     void main(string[] args) {
  14.         writeln("Main: The meaning of life is ", meaningOfLife());
  15.     }
  16. }
  17.        
  18. #!/usr/bin/env rdmd -version=test
  19.  
  20. import scriptedmain;
  21. import std.stdio;
  22.  
  23. version (test) {
  24.     void main(string[] args) {
  25.         writeln("Test: The meaning of life is ", meaningOfLife());
  26.     }
  27. }
  28.        
  29. $ ./scriptedmain.d
  30. Main: The meaning of life is 42
  31. $ ./test.d
  32. Test: The meaning of life is 42
  33. $ dmd scriptedmain.d -version=scriptedmain
  34. $ ./scriptedmain
  35. Main: The meaning of life is 42
  36. $ dmd test.d scriptedmain.d -version=test
  37. $ ./test
  38. Test: The meaning of life is 42