pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

D paste-bin - collaborative debugging tool View Help


Posted by grabrelif on Sat 9th Jun 09:24
download | new post

  1. #!/usr/bin/dmd -run
  2. /* sh style script syntax is supported */
  3.  
  4. /* Hello World in D
  5.    To compile:
  6.      dmd hello.d
  7.    or to optimize:
  8.      dmd -O -inline -release hello.d
  9. */
  10.  
  11. import std.stdio;
  12.  
  13. void main(char[][] args)
  14. {
  15.     writefln("Hello World, Reloaded");
  16.  
  17.     // auto type inference and built-in foreach
  18.     foreach (argc, argv; args)
  19.     {
  20.         // Object Oriented Programming
  21.         CmdLin cl = new CmdLin(argc, argv);
  22.         // Improved typesafe printf
  23.         writefln(cl.argnum, cl.suffix, " arg: %s", cl.argv);
  24.         // Automatic or explicit memory management
  25.         delete cl;
  26.     }
  27.  
  28.     // Nested structs and classes
  29.     struct specs
  30.     {
  31.         // all members automatically initialized
  32.         int count, allocated;
  33.     }
  34.  
  35.     // Nested functions can refer to outer
  36.     // variables like args
  37.     specs argspecs()
  38.     {
  39.         specs* s = new specs;
  40.         // no need for '->'
  41.         s.count = args.length;       // get length of array with .length
  42.         s.allocated = typeof(args).sizeof; // built-in native type properties
  43.         foreach (argv; args)
  44.             s.allocated += argv.length * typeof(argv[0]).sizeof;
  45.         return *s;
  46.     }
  47.  
  48.     // built-in string and common string operations
  49.     writefln("argc = %d, " ~ "allocated = %d",
  50.         argspecs().count, argspecs().allocated);
  51. }
  52.  
  53. class CmdLin
  54. {
  55.     private int _argc;
  56.     private char[] _argv;
  57.  
  58. public:
  59.     this(int argc, char[] argv) // constructor
  60.     {
  61.         _argc = argc;
  62.         _argv = argv;
  63.     }
  64.  
  65.     int argnum()
  66.     {
  67.         return _argc + 1;
  68.     }
  69.  
  70.     char[] argv()
  71.     {
  72.         return _argv;
  73.     }
  74.  
  75.     char[] suffix()
  76.     {
  77.         char[] suffix = "th";
  78.         switch (_argc)
  79.         {
  80.           case 0:
  81.             suffix = "st";
  82.             break;
  83.           case 1:
  84.             suffix = "nd";
  85.             break;
  86.           case 2:
  87.             suffix = "rd";
  88.             break;
  89.           default:
  90.             break;
  91.         }
  92.         return suffix;
  93.     }
  94. }

Submit a correction or amendment below. (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Use syntax highlighting

To highlight particular lines, prefix each line with @@
:
:
Remember my settings