Dè¨èªåºç¤ææ³æéãã¹ã¿ã¼
注æï¼
ãµããã®ã§æ¸ãç´ãã¾ããï¼https://gist.github.com/2470712 ã«ããæ°ããæ¹ãããããã ããï¼
以éå¤ãç
ä»ã®è¨èªãããç¨åº¦ç¥ã£ã¦ãã人ã¯ãããèªãã°Dè¨èªã®åºç¤ããã¹ã¿ã¼ã§ãï¼Dè¨èªã®æ°·å±±ã®ä¸è§ãããã¯ç¥ããã¨ãã§ããã¨æãã¾ãï¼å¯¾è±¡ãã¼ã¸ã§ã³ã¯dmd 2.040ã§ãï¼
1. åºç¤
ã½ã¼ã¹ãã¡ã¤ã«
ASCIIãUTFããåãä»ãã¾ããï¼ãã以å¤ã®æåã³ã¼ãã§æ¸ãã¨ã³ã³ãã¤ã«ã¨ã©ã¼ã«ãªãã¾ãï¼
main
Dè¨èªã®mainã¯Cã¨ã¯éã以ä¸ã®ãããªã·ã°ããã£ã§ãï¼
void main(); void main(string[] args);
ãªã¿ã¼ã³ã³ã¼ãã«ã¤ãã¦ã¯å¦çç³»ããããªã«ãã£ã¦ããã¾ãï¼
表示 (write(ln), writef(ln))
æ¨æºã©ã¤ãã©ãªã«ããstd.stdioã使ãã¾ãï¼
import std.stdio; ... write(1); // æ¹è¡ç¡ã writeln(1); // æ¹è¡æã writef("%02d", 1); // ãã©ã¼ãããæå®
å¤æ°ã®å®£è¨
Dè¨èªã§ã¯"å å¤æ°å"ã¨ããããã«å®£è¨ãã¾ãï¼
int a; // ããã©ã«ãå¤ã§åæåãããï¼intã¯0 int a = void; // åæåãããï¼Cãªã©ã¨åããã´ããå ¥ã£ã¦ãã
ã¾ãï¼åæååããåæ¨è«ããããã®autoã¨ããäºç´èªãããã¾ãï¼
auto a = "auto"; // aã¯immutable(char)[]å
ä»ã®ä¿®é£¾åãããå ´åãåæ¨è«ãå¹ãã¾ãï¼
const a = "const";
ãã¼ã¿å
å¤åã¨åç §åãããã¾ãï¼ä»¥ä¸ã¯å¤åã®ä¸ä¾ã§ãï¼
// ãã¼ã«å¤ bool flag; // 符å·ãªã8bitã®UTF-8æå(ãã«ããã¤ããããªãã®ã§æ³¨æ) char c; // æ°å¤ã¯Cã¨ã¯éããµã¤ãºåºå®ã§ãï¼ byte num1; // 8bit short num2; // 16bit int num3; // 32bit double num4; // 64bit // éçé å int[5] arr; // æ§é ä½ Random random;
åç §åã¯è¤åçãªãã¼ã¿æ§é ãªã©ãå½ã¦ã¯ã¾ãã¾ãï¼
// åçé å int[] arr; // ãªãã¸ã§ã¯ã Object obj; // ããªã²ã¼ã void delegate(void) action;
ã³ã¡ã³ã
4種é¡ããã¾ãï¼
// ä¸è¡ã³ã¡ã³ã /* è¤æ°è¡ã³ã¡ã³ã */ /** DDocã³ã¡ã³ã */ /+ /+ ãã¹ãå¯è½è¤æ°è¡ã³ã¡ã³ã +/ +/
ããã°ã©ã ã®å®è¡
$ dmd foo.d $ ./foo
runãªãã·ã§ã³ä½¿ãã¨å®è¡ãã¡ã¤ã«ãªã©ãçæããã«å®è¡ã§ãã¾ãï¼
$ dmd -run foo.d
2. æ°å¤
æ´æ°ï¼uãã¤ãã¨ç¬¦å·ãªãã«ãªãã¾ãï¼
long num = -1; // 符å·ã¤ã ulong num = 100_000_000; // 符å·ãªã
double num = 1.234; real num = 5.678; // ç¾ç¶å¯ä¸ã®ãã¼ãã¦ã§ã¢ä¾å(x86 CPUãªã80bit)
è¤ç´ æ°ã使ããããã¾ãï¼
creal num = 1 + 2i; num += 3i; writeln(num.re, num.im); // reãå®é¨(1)ï¼imãèé¨(5)
ååæ¼ç®
// numã¯intã¨ãã num = 1 + 1; num = 1 - 1; num = 1 * 2; num = 5 / 2; // 2 num = 5 % 2; // 1
æ¼ç®åã®ã©ã¡ãããæµ®åå°æ°ç¹æ°ã®å ´åï¼çµæãæµ®åå°æ°ç¹æ°ã«ãªãã¾ãï¼
// numã¯doubleã¨ãã num = 5.0 / 2; // 2.5(numãintãªã©ã®æ´æ°åã ã¨ã³ã³ãã¤ã«ã¨ã©ã¼)
ã¤ã³ã¯ãªã¡ã³ãã¨ãã¯ãªã¡ã³ã
å¿è«ããã¾ãï¼
i++; --i;
3. æåå
æååã¯ããã«ã¯ã©ã¼ãã§å²ã¿ã¾ãï¼ããã«ã¯ã©ã¼ãã®ä¸ã§ã¯\t(ã¿ã)ã\n(æ¹è¡)ãªã©ã®ç¹æ®æåãå©ç¨ãããã¨ãã§ãã¾ãï¼
string str1 = "abc"; string str2 = "a\tbc\n";
Dè¨èªã§ã®æååã¯4ã§è¿°ã¹ãé åã®ä¸ç¨®ã«éãã¾ãã(stringã¯immutable(char)[]ã®aliasã§ã)ï¼ã¾ããã¹ããã£ãã¯ã¹ãã¤ãããã¨ã§ãªãã©ã«ã®åãæå®ã§ãã¾ãï¼
string str3 = "hello"c // åæåã¯charå wstring str4 = "hello"w // åæåã¯wcharå dstring str5 = "hello"d // åæåã¯dcharå
æååæä½
// çµå auto str = "aaa" ~ "bbb"; // é·ã(ãã¤ã) auto length = "abcdef".length; // åãåºã auto substr = "abcd"[0..2]; // "ab" /* ãã以éã®ãã®ã¯std.stringãå¿ è¦ã§ã */ // åå² auto record = "aaa,bbb,ccc".split(","); // æ¤ç´¢ auto idx = "abcd".indexOf("bc"); // è¦ã¤ãã£ãå ´åã¯ãã®ä½ç½®ï¼è¦ã¤ãããªãã£ãå ´åã¯-1
4. é å
é åã¯ã[]ãã使ãã¾ãï¼éçé åã¨åçé åãããã¾ããï¼ãã使ãããåçé åã«ã¤ãã¦æ¸ãã¾ãï¼
int[] arr = [100, 200, 300];
宣è¨ã§ã¯Cã¨ã¯éãå置形å¼ã¨ãªãã¾ãï¼
è¦ç´ ã®åç §ã¨ä»£å ¥
// åç § a = arr[0]; b = arr[1]; c = arr[5]; // Error! è¦ç´ æ°ããå¤ãã¨ä¾å¤ãæãããã // ä»£å ¥ arr[0] = 1; arr[1] = 2; arr[5] = 5; // åç §ã¨åãã
è¦ç´ ã®åæ°
len = arr.length; // è¦ç´ ãå¢ãã(å¢ããåã¯ããã©ã«ãå¤ã§åãããã) arr.length += 10;
é åã®æä½
std.arrayã使ãã¨Dè¨èªã§ã®æ¨æºçãªã¤ã³ã¿ã¼ãã§ã¤ã¹(Range)ãå©ç¨ã§ãã¾ãï¼
import std.array; auto arr = [1, 2, 3]; // å é ãåå¾ auto a = arr.front; // aã¯1 // å é ãåé¤ arr.popFront; // arrã¯[2, 3] // å é ã«è¿½å (pushç³»ããªãã®ã§insertã§) arr.insert(0, 5); // arrã¯[5, 2, 3] // æ«å°¾ãåå¾ auto b = arr.back; // bã¯3 // æ«å°¾ãåé¤ arr.popBack; // arrã¯[5, 2] // æ«å°¾ã«è¿½å arr ~= 9; // arrã¯[5, 2, 9]
popFrontãpopBackã§å¤ãè¿ããªãã®ã¯ä¾å¤å®å ¨ã®ããã§ãï¼
ãã¯ãã«æ¼ç®
ãã¡ãã¡ã«ã¼ãã¨ã使ãå¿ è¦ããã¾ããï¼[]ã使ããã¨ã§ãã¯ãã«æ¼ç®ã§ãã¾ãï¼
auto a = [1, 2, 3]; a[] += 10; // [11, 12, 13]
5. é£æ³é å
é£æ³é åãã[]ãã使ãã¾ãï¼ãã¼ã¨å¤ã:ã§åºåãã¾ãï¼
int[string] hash = ["a" : 1, "b" : 2];
è¦ç´ ã®åç §ã¨ä»£å ¥
// åç § hash["a"] // 1 hash["b"] // 2 hash["z"] // é åã¨åããä¾å¤ãæãããã // ä»£å ¥ hash["c"] = 5 hash["d"] = 7
é£æ³é åã®æä½
// ãã¼ã®åå¾ hash.keys; // ["a", "b", "c", "d"] // å¤ã®åå¾ hash.values; // [1, 2, 5, 7] // ãã¼ã®åå¨ç¢ºèª auto val = "a" in hash; // valã«ã¯1ã¸ã®ãã¤ã³ã¿ï¼ãªããã°null // ããã·ã¥ã®ãã¢ã®åé¤ hash.remove("a");
6. å¶å¾¡æ
ifæ
if (cond) { // do something }
if ã elseæ
if (cond) { // do something } else { // do something }
if ã else if æ
if (cond) { // do something } else if (cond) { // do something }
switchæ
Cã¨ã¯éãï¼æååã使ãããcaseã並ã¹ã¦æ¸ããã¨ãåºæ¥ã¾ãï¼
switch (command) { case "foo", "bar": // do something break; case "baz": // do something break; default: }
whileæ
uint i; while (i < 5) { // do something ++i; }
foræ
for (uint i; i < 5; i++) { // do something }
foreachæ
é åãï¼opApply/Rangeã¤ã³ã¿ã¼ãã§ã¤ã¹ãå®è£ ãã¦ãããªãã¸ã§ã¯ããå¦çåºæ¥ã¾ãï¼
foreach (elem; arr) { // do something }
7. é¢æ°/ããªã²ã¼ã
é¢æ°ã¯Cã¨åããããªãã®ã§ããè²ã ã¨æå®ã§ãã¾ãï¼xã¯constãªå¤ã¨ãã¦å ¥åã表ãï¼refã¯åç §ã¨ãã¦æä½ãããã¨ã表ãã¾ãï¼lazyã¯é 延è©ä¾¡(zãé¢æ°å ã§ä½¿ãããã¾ã§è©ä¾¡ãé 延ããã)ãè¡ãã¾ãï¼
nothrow void foo(in int x, ref int y, lazy int z = 0) { // do something }
ããªã²ã¼ãã¯ãã¹ããããé¢æ°ãªã©ã該å½ãã¾ãï¼ããã¯ã¯ãã¼ã¸ã£ã®åãããã¾ãï¼
uint delegate() createCounter() { uint count; return { return ++count; }; // {}ã¯delegateãªãã©ã«(å¼æ°ããªãã®ã§()ã¯çç¥) } auto counter = createCounter(); writeln(counter()); // 1
8. ãã¡ã¤ã«å ¥åºå
std.stdioã«ããFileã使ãã¾ãï¼ä»¥ä¸ã¯ã³ãã¼ã®ä¾ã§ãï¼
auto fin = File("orig.txt"); // ããã©ã«ãã¯èªã¿è¾¼ã¿ auto fout = File("copy.txt", "w"); // æ¸ãè¾¼ã¿ã¢ã¼ãã§ãªã¼ãã³ foreach (line; fin.byLine) fout.write(line); // Fileã¯åç §ã«ã¦ã³ãã§ç®¡çããã¦ããã®ã§æ示çãªcloseã¯ä¸è¦
ç¥ã£ã¦ãããæ¹ãããææ³
Dè¨èªã®çå½å¤
nullï¼falseï¼æ°å¤åã®0ï¼ã¾ã å²ãå½ã¦ããã¦ããªã空ã®é åï¼ã¯å½ã¨ãªãã¾ãï¼æå¾ã®é åã®æåã¯ã©ãã«ãæªããããï¼é åé¢ä¿ã¯emptyã使ã£ã¦è©ä¾¡ãããã¨ããªã¹ã¹ã¡ãã¾ãï¼
é åã第ä¸å¼æ°ã«åãé¢æ°
第ä¸å¼æ°ãé åã®å ´åã«ã¯ã·ã³ã¿ãã¯ã¹ã·ã¥ã¬ã¼ãç¨æããã¦ãã¾ãï¼
void foo(int[] arr) {} int[] arr = [1, 2, 3]; // 以ä¸ã¯ã©ã£ã¡ã§ãOK foo(arr); arr.foo;
std.arrayãstd.stringã使ã£ãé¢æ°ãvar.methodã®ããã«å¼ã¹ãã®ã¯ãã®ããã§ãï¼
ã¯ã©ã¹
Javaã¨åçã®æ©è½ãæä¾ãã¦ãã¾ã(åä¸ç¶æ¿ã§ã)ï¼
class Person { private: // 以éã¯private string name_; public: // 以éã¯public @property { string name() const { return name_; } void name(string name) { name_ = name; } } this(string name) // ã³ã³ã¹ãã©ã¯ã¿ã¯this { name_ = name; } ... } // Cãªã©ã¨éã;ã¯ãããªã
ãã®ä»ã«ãinterfaceï¼PODã¨ãã¦æ±ããstructãunionï¼æ¼ç®åãªã¼ãã¼ãã¼ããªã©ãããã¾ãï¼ã¾ãï¼Dè¨èªã¯GCã使ã£ã¦ã¡ã¢ãªç®¡çãã¦ããããï¼newããå¾deleteããå¿ è¦ã¯ããã¾ãã(èªãmallocãªã©ããå ´åã¯å¿è«freeã¯å¿ è¦ã§ã)ï¼
ãã³ãã¬ã¼ã
C++ãããã¯ãã£ããæ¸ããããã«ãªã£ã¦ã¾ãï¼ä»¥ä¸ã¯éä¹ãè¨ç®ãããã³ãã¬ã¼ãã§ãï¼
template factorial(int n) { static if (n == 1) enum factorial = 1; else enum factorial = n * factorial!(n-1); } /* C++ãªã©ã¨ã¯éã<>ã使ãã!()ã使ã */ factorial!(5) // 120
å¿è«ã¯ã©ã¹ãé¢æ°ã§ã使ãã¾ãï¼
ä¾å¤å¦ç
Mutex(å¤æ°m)ãä¾ã«ï¼ã¾ãã¯ããç¥ããã¦ããtry - catch - finallyï¼
lock(m); try { // do something } finally { unlock(m); }
ã¹ã³ã¼ãã¬ã¼ãæã使ã£ãæ¹æ³ãããã¾ãï¼è§£æ¾å¦çãã¨ã¦ãè¿ããªãï¼ã³ã¼ããè¦ããããªãã¾ãï¼
lock(m); scope(exit) unlock(m); // ã©ããªçç±ã§ããï¼ã¹ã³ã¼ããåºãæã«å¼ã°ãã // do something
RAIIã®ãããªæ¹æ³ãå¯è½ã§ãï¼scopeã§å®£è¨ãããå¤æ°ã¯ã¹ã³ã¼ããæããæã«ç ´æ£ããï¼ãã¹ãã©ã¯ã¿ãå¼ã³åºãã¾ãï¼
class Lock { Mutex m_; this(Mutex m) { m_ = m; lock(m_); } ~this() { unlock(m_); } // ãã¹ãã©ã¯ã¿ã¯~this } scope myLock = new Lock(m); // do something
Range
Dè¨èªã®æ¨æºã©ã¤ãã©ãªã¯ä»ãã®ã³ã³ã»ãããä¸å¿ã«éçºããã¦ãã¾ãï¼æ ¸ã¨ãªãstd.rangeã«æºããã¹ãã¤ã³ã¿ã¼ãã§ã¤ã¹ãå®ç¾©ããã¦ãã¾ãï¼
import std.algorithm; auto arr = [1, 2, 3, 4, 5]; // æ¡ä»¶ã«åããã®ã ããé¸ã¶ filter!("a % 2 == 0")(arr); // [2, 4] // æ¡ä»¶ã«åããã®ãé¤ã remove!("a % 2 == 0")(arr); // [1, 3, 5] // æ¡ä»¶ã«åããã®ã®åæ°ãæ°ãã count!("a % 2 == 0")(arr); // 2 // å å·¥çµæãRangeã§è¿ã map!("a * a")(arr); // [1, 4, 9, 16, 25] // éé ã«ã½ã¼ããã sort!("a > b")(arr); // [5, 4, 3, 2, 1]
8ã§ä¸ããFileãªã©ãå«ãï¼ä»ã®ã¢ã¸ã¥ã¼ã«ãRangeãã¼ã¹ã¨ãªã£ã¦ãã¾ãï¼
åä½ãã¹ã
unittestã§å²ã£ãæã«ãã¹ããããã¾ãï¼ã¯ã©ã¹å ã§ãæ§ããªãã®ã§ï¼ããè¿ãã«æ¸ãã®ãDè¨èªã®ä½æ³ã§ãï¼
unittest { bool foo() { /* do something */ return flag; } assert(foo()); }
ããã¯ã³ã³ãã¤ã«æã«unittestãªãã·ã§ã³ã渡ããã¨ã§å®è¡ãããããã«ãªãã¾ãï¼
çµããã«
æ¬ãéãã¦ãã¾ã£ãæ°ã¯ãã¾ããæ¸ãã¦ã¿ã¾ããï¼template/å¥ç´/TLS/åã·ã¹ãã ãããå«ãï¼å ¨ç¶é åãæ¸ãã¦ãªãæ°ããã¾ããåºæ¬ã¨ãããã¨ã§ã¾ããããªæãã§è¨±ãã¦ä¸ããï¼Dè¨èªã¯é²åãæ©ãè¨èªãªã®ã§ï¼ãã®è¨äºããã¤ä½¿ããªããªãã®ãã¡ãã£ã¨å¿é ã ã£ãããã¾ãâ¦