Last update April 28, 2009

Short Frequent Answers


Here is a short list of common answers to common questions asked about D. Reading the list should help programmers (particularly C/C++) quickly get to know D.

  • Strings are not null-terminated but hold explicit length information. Therefore you need to use %.*s not %s in printf, or just use writef!
  • Save yourself a bunch of trouble and don't use printf(). It's not type-safe, it doesn't play nice with D's strings, and it has no concept of classes (so it can't call Object.toString()). Use D's writef() or writefln() instead, which are in the std.stdio module.
  • Background: dynamic arrays (e. g. strings) and array slices are implemented as 8-Byte-(length, pointer)-dupels. So, the length is stored "in" the reference, or belongs to it. (but no capacity like in c++ vectors is stored).
  • Visibility (public/private/protected) is on the module level not the scope/block level. There is also a package protection.
  • To create an instance of a class you need to use the word *new* otherwise the reference will be null.
  • Casting in D requires that the word *cast* be appended in front of the bracketed conversion type. ie cast(int)
  • If your switch(expr) statement doesn't have a default case and expr doesn't match any of the cases, an exception is thrown when in debug mode.
  • Source code must be in either UTF-8 (or other Unicode), or ASCII (a subset of UTF-8). Specifically, ISO-8859-1 ("Latin-1") will *not* work!
  • The console / terminal must be set to UTF-8, or non-ASCII characters will not be handled correctly.
  • Comparing an object reference like: "if (object == null)" will crash. You must use "if (object is null)". This operator is necessary, because == could be overloaded.
  • Checking for a key in an AA like: "if(array[key])" will create it if it's missing, or give an error as of dmd 0.126. You must use "if(key in array)".
  • In a do/while loop, the keyword "continue" will jump to the condition, not the start of the loop body.
For more details see Error Messages and the F.A.Q..

FrontPage | News | TestPage | MessageBoard | Search | Contributors | Folders | Index | Help | Preferences | Edit

Edit text of this page (date of last change: April 28, 2009 22:36 (diff))