D for Haskeller
http://haskell.g.hatena.ne.jp/mr_konn/20061006/1160138831
å¿è«すごい人ã®çä¼¼ãªè¨³ã§ããã
C++ããDã®ã»ããåªãã¦ãããã ã¨ä¸»å¼µãããã®ã§æ¸ãã¾ãã
対象èªè
ã¯ããã¿ããã¿ã¨è¦æãã人ã§ãã
ã¨ãããããこちらãã¨æ¯è¼ããªããèªã¿ã¾ãããã
ã¾ãã¯å¤æ°ããªãã¨Dã«ã¯C++ã«ã¯ãªãåæ¨è«ãããã¾ãã
const n = 10;
ããã§nã¯æ´æ°åã¨æ¨è«ããã¾ããç´ æ´ãããã
次ã«é£æ³é åããã¬ã¼ã¹ã®å¾ã®ã;ãã¨staticãæ¸ããªãã¦ãããã¨ããç¹ã§C++ãããæ¸ãããããªã£ã¦ãã¾ãã
struct AssocArray{ const n = 10; const m = n+10; const b = true; }
ãããªæãã§ä½¿ãã¾ããã::ããããã.ãã®æ¹ãå¿è«1byteååªãã¦ãã¾ãããã
const z = AssocArray.n + AssocArray.m;
å¥ã®ååã«æç¸ããæã¯ãaliasã使ãã¾ããtypedefããã2byteãçãã§ãã
alias AssocArray AssocArray2;
ããã¦é¢æ°ããããç´ æ´ãããã§ãããªãã¨Dã®é¢æ°ã¯é£æ³é
å以å¤ã®åãè¿ãã¾ãã
template add_int(int n, int m){ const add_int = n + m; }
é¢æ°å¼ã³åºãã¯ä»¥ä¸ã®ããã«ãã¦è¡ãã¾ãã
const n = 10; const m = 20; const o = add_int!(n, m);
ã!(...)ãã¨ããå½¢å¼ã¯è¦æ
£ãã¦ããªãããç¥ãã¾ãããããããã<...>ãããã¯é¢æ°ãå¼ã³åºãã¦ãããã¨ããæããããã®ã§ã¯ãªãã§ããããã
ä½ãããC++å½¢å¼ã§ã¯é¢æ°ã®å¼æ°ã«é¢æ°ã®è¿å¤ã渡ãã¦ããã¨ãã«ã
add_int<3, add_int<1,2> >; // >ã¨>ã®éã«ã¹ãã¼ã¹ãå¿ è¦ï¼
ã¨ãã£ãåé¡ãããã¾ãããDã§ã¯ãã®ãããªåé¡ã¯ããã¾ãããDã¯å®ç§è¶
絶è¨èªã§ãã
ã§ã¯æ©éãé¢æ°ã®æ¸ãæ¹ã«ã¤ãã¦èª¬æãã¾ãã
template // é¢æ°ã§ãããã¨ã示ããã¼ã¯ã¼ã (int n, int m) // å¼æ°ã¯ n::int, m::int ã®ãµã㤠add_int // é¢æ°å { const add_int // å¿ ãé¢æ°åã¨åãåãæã¤å¤æ°ãä½ã = n+m; // ããã«é¢æ°ã®æ»ãå¤ãä»£å ¥ãã¾ã }
é常ã«ç´æçã§ãå®ã«åãããããã§ãããç´ æ´ãããã§ããåå¼ã
å¿è«ãã¿ã¼ã³ãããã使ãã¾ãã
template add_int(int n, int m){ const add_int = n + m; } template add_int(int n : 10, int m : 10){ const add_int = 40; }
å¼æ°ã®å¾ãã«ã:ããã¤ãã¦ããã®å¾ã«å¤ãæ¸ãã¦ããã ãï¼ãããC++ããç解ããããã¦åªãã¦ãã¾ããã
ããã¦éä¹è¨ç®ã
template fact(int n){ const fact = fact!(n-1) * n; } template fact(int n : 1){ const fact = 1; }
ç¾ããâ¦
ããã¦æå¾ã¯Hello Worldã§ãã
void main(){ printf("Hello World.\n"); }
ãã¼ãããã£ããï¼
ç¶ãï¼ãé¡ããã¾ãï¼