Created
January 31, 2014 12:26
-
-
Save CromFr/8731168 to your computer and use it in GitHub Desktop.
Notes on D programming
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Notes D | |
"===================================== Types:" | |
auto a = 12; | |
// Same as c++11 auto | |
<type>.stringof | |
// Name of the type | |
<type>.sizeof | |
// size in Bytes of the type | |
<type>.min | |
<type>.max | |
// Min and Max values alowed by the type | |
<type>.init | |
// Initial value of the type | |
typeof(var) var2; | |
// var2 is the same type of var | |
typeof(var).stringof // The name of the type of var | |
immutable <type> var = something; | |
// Var will never change | |
// Its value can be calculated at runtime | |
lazy <type> var; | |
// Allocated at first use, deleted (right?) after | |
to!<type>(value) | |
// Converts value to the given type | |
// Can throw exception if conversion is impossible | |
//Tuple | |
import std.typecons; | |
auto tup = Tuple!(int, string)(42, "hello");//new tuple | |
tup[0];//Access to 42 | |
tup[1];//Access to hello | |
//Named Tuple | |
auto t = Tuple!(int,"number", string,"message")(42, "hello"); | |
tup.number;//Access to 42 | |
tup.message;//Access to hello | |
"===================================== Enumerations:" | |
enum NaturalConstant : double | |
{ | |
pi = 3.14, | |
e = 2.72 | |
} | |
enum TemperatureUnit : string | |
{ | |
C = "Celsius" , | |
F = "Fahrenheit" | |
} | |
enum int secondsPerDay = 60 * 60 * 24; | |
// Single value enum (and it is immutable) | |
"===================================== Operators:" | |
a^^b | |
// a power b | |
objA is objB | |
// Checks if objA is the object as objB (same allocated object) | |
//Overload | |
ref MyClass opUnary(string op) | |
{ | |
if(op=="++"){ | |
//... | |
return this; | |
} | |
} | |
// idem with opCast, opBinary, ... | |
"===================================== Error reporting" | |
assert(bCond, sMsg); | |
// Exits the program with sMsg if bCond is false | |
static assert(bCond, sMsg); | |
// Same as assert but at compile time | |
throw new Exception(sExc); | |
// Throw exception with sExc as message | |
"===================================== Streams" | |
<stream>.writeln(...) | |
// Write to the given stream (stdout, stderr, stdlog, ...) | |
"===================================== Arrays:" | |
<type>[<size>] array; | |
// Fixed size array | |
array = [ a, b, c, d, ...];//Fill array | |
<type>[<keytype>] array; | |
// Dynamic array with keytype as key (default size_t) | |
array.length = 10;//Resize array | |
array.sort;//Sort array | |
array.reverse;//sort array in reverse order | |
array ~= a;//Append a to array | |
key in array;//true if the array contains an element with the given key | |
array.get(key, defval);//Returns the value at the given key, defval if not found | |
array.remove(key)//Deletes the element at the given key | |
array[0..3];//Three first elements of array | |
array[];//The entire array | |
$ | |
// In array operations, $ designate the index pf the last cell | |
// /!\ Operator= create reference. to copy, use array.dup | |
// See also capacity to get ??the count of cells owned by the array?? | |
// if capacity=0 ==> Appending to it will reallocate everything | |
// else ==> number of new cells that can be added before reallocation | |
// cf c++ vector block allocation | |
foreach(<indextype> index, <valuetype> value ; array){} | |
// Index can be ignored, and types too | |
"===================================== Strings:" | |
str.toLower | |
str.toUpper | |
// Modifies str | |
// (from std.uni) | |
char[] cstr = "hello".dup; | |
cstr.idup;//Produces a string of cstr | |
string s = q{int number = 42; ++number;}; | |
// S contain the D code inside q{} | |
string s = `c:\nurten`; | |
// Skips character escape | |
// | |
string s = r"$.*[a|b]^"; | |
// Content for regex | |
format(str, value1, value2, ...) | |
// Returns the str completed with values (as printf) | |
"===================================== Environment:" | |
import std.getopt; | |
getopt(args, "MyOpt",&value, "MyOpt2|mo2",&value2); | |
// Returns the --MyOpt=xxx option given by command line | |
import std.process; | |
getenv(VarName) | |
// Returns the value of the given environment variable | |
"===================================== Unit tests:" | |
//You must build with -unittest | |
unittest | |
{ | |
assert(SomeFunc(params) == something ); | |
assert(SomeFunc(params2) == something2 ); | |
//... | |
} | |
//unittest executed before program starting | |
"===================================== Contract Programming:" | |
//Build with -release to disable CP | |
int func(p1, p2) | |
in{ | |
//Check executed when func is called | |
} | |
out(int ret){ | |
//Check executed when func has terminated | |
} | |
body{ | |
//Code | |
} | |
class A{ | |
invariant(){ | |
//Called when something happen on the object | |
//Checks object consistency | |
} | |
} | |
"===================================== Class:" | |
class MyClass : SuperClass, Interface, ... { | |
this(){}//Constructor | |
~this(){}//Destructor | |
this(this){}//Copy construct | |
opAssign(MyClass){}//Assign operator (=) | |
// super : mother class | |
// | |
override funcove(){}//Make sure it is overridden | |
abstract funcabs(){}// abstract func | |
final funcfin(){}// final func | |
@property{ | |
void prop(int newvalue){}//Will be called with MyObject.prop = 50; | |
int prop(){return xxx;}//will be called with MyObject.prop; | |
} | |
} | |
//Multiple inheritance | |
class platypus{ | |
this(){ | |
ovididentity = new Ovid; | |
mammaldentity = new Mammal; | |
} | |
Ovid ovididentity; | |
Mammal mammaldentity; | |
alias Ovid this; | |
alias Mammal this; | |
}//See programming in D page 414 | |
"===================================== Templates:" | |
T func(T = <defaulttype>)(T arg1, ...){} | |
// Template function | |
// Call it with: | |
// func(value) | |
// func!<type>(value) | |
//Specialization | |
void func(T:string)(T arg1, ...){} | |
// string specialization | |
class MyClass(T){ | |
//template class | |
} | |
"===================================== Conditional Compilation:" | |
debug{ | |
//Compiled if -debug specified to compiler | |
} | |
else{ | |
//Compiled if -debug not specified to compiler | |
} | |
debug(tag){ | |
//Compiled if -debug=tag is specified to compiler | |
} | |
debug(2){ | |
//Compiled if -debug=2/-debug=3/-debug=4/... is specified to compiler | |
//not if -debug=1 | |
} | |
version(){ | |
//Same as debug | |
} | |
//Append new tag: | |
debug(something) | |
{ | |
debug = binarySearch;//Add debug tag | |
debug = stackContainer;//Add debug tag | |
version = testRelease;//Add version tag | |
version = schoolRelease;//Add version tag | |
} | |
////Predefined version tags: | |
// | |
//Compiler: DigitalMars GNU LDC SDC | |
//OS: Windows Win32 Win64 linux OSX Posix FreeBSD OpenBSD NetBSD DragonFlyBSD BSD Solaris AIX Haiku SkyOS SysV3 SysV4 Hurd | |
//CPUEndian: LittleEndian BigEndian | |
//CompilerSwitches: D_Coverage D_Ddoc D_InlineAsm_X86 D_InlineAsm_X86_64 D_LP64 D_PIC D_X32 D_HardFloat D_SoftFloat D_SIMD | |
// D_Version2 D_NoBoundsChecks unittest assert | |
//CPU: X86 X86_64 | |
//Platform: Android Cygwin MinGW ARM ARM_Thumb ARM_Soft ARM_SoftFP ARM_HardFP ARM64 PPC PPC_SoftFP PPC_HardFP PPC64 IA64 | |
// MIPS MIPS32 MIPS64 MIPS_O32 MIPS_N32 MIPS_O64 MIPS_N64 MIPS_EABI MIPS_NoFloat MIPS_SoftFloat MIPS_HardFloat | |
// SPARC SPARC_V8Plus SPARC_SoftFP SPARC_HardFP SPARC64 S390 S390X HPPA HPPA64 SH SH64 Alpha Alpha_SoftFP | |
// Alpha_HardFP | |
static if(double.sizeof == 4){ | |
//Compiled if double.sizeof == 4 | |
} | |
"===================================== Others:" | |
uniform(min, max); | |
// random ?integer? between min and max | |
alias MyList = List!MyItem; | |
// Mylist will be a List with template MyItem | |
destroy(MyObject) | |
// Calls destructor | |
pure int MyFunc(params)//Modifies only its parameters and return value, nothing else will be modified | |
nothrow void myfunc(params)//Will never throw exceptions | |
public import xxx; //recursive import | |
private import xxx; //Encapsulated import (default) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment