08用d编程域.

不能在内部域中定义与外部域中相同的名字.
有的在域的最前定义变量
一般在使用前定义变量,而不是在之后.//找不到,也不规范
最好在刚要使用前定义,在速度,不犯错,可读,代码维护上都不错.

import std.stdio;
void main(){
for ({ int i = 0; double d = 0.5; } i < 10; ++i) {
    writeln("i: ", i, ", d: ", d);
    d /= 2;
}
}

在{}中声明多种类型变量
d官方编程风格
?:,三元符.三个表达式,叫三元符.
真式/假式的类型不必相同,但必须要有公共类型.计算涉及类型转换/继承,结果还可能是左值/右值
确定表达式类型简单方法:typeof(...).stringof.
十进制可用下划线数字分隔符,可千分可万分,第1个字符不能为0,0x为16进制,8进制,可在std.conv里面用octal!541等初化.因为不常用,所以从语言转向库里面了.
二进制,0b/0B

import std.stdio;

void main() {
    writeln("\n--- these are written in decimal ---");

    // fits an int, so the type is int
    writeln(       2_147_483_647, "\t\t",
            typeof(2_147_483_647).stringof);

    // does not fit an int and is decimal, so the type is long
    writeln(       2_147_483_648, "\t\t",
            typeof(2_147_483_648).stringof);

    writeln("\n--- these are NOT written in decimal ---");

    // fits an int, so the type is int
    writeln(       0x7FFF_FFFF, "\t\t",
            typeof(0x7FFF_FFFF).stringof);

    // does not fit an int and is not decimal, so the type is uint
    writeln(       0x8000_0000, "\t\t",
            typeof(0x8000_0000).stringof);

    // does not fit a uint and is not decimal, so the type is long
    writeln(       0x1_0000_0000, "\t\t",
            typeof(0x1_0000_0000).stringof);

    // does not fit a long and is not decimal, so the type is ulong
    writeln(       0x8000_0000_0000_0000, "\t\t",
            typeof(0x8000_0000_0000_0000).stringof);
}

L后缀表.U/u后缀表,UL/LU正长,不用l是避免与1搞混.
浮点可用16进制表示:如0x9a.bc.
十进制中e/E表以10为底的幂.可正可负.
16进制中p/P表以2为底的幂.0xabc.defP4相当于*16=2^^4.
对浮点.默认为双精,f/F为浮,L实(80位,10字节)
字符.\表转义.
常见\\,\a(叫),\n(下行),\r(回车),\t(制表),\v(垂直制表),\f新页.
‘42’,’\x21’,’\u…’,’\U…’,
类似超文本的,’&euro;’,’&heart;’,’&copy;’,更多
双引号字符里面可以包括以上.
反引号:所见即所得串.r"…",也是所见即所得

    import std.stdio,std.conv;
    writeln(`c:\nurten`);
    writeln(r"c:\nurten");//如上一样
    writeln(hexString!"44 64 69 6c 69");
    writeln("c:\nurten");

“\x44\x64\x69\x6c\x69”,与(hexString!"44 64 69 6c 69")一样,x"...."已经过时了.

writeln(q"MY_DELIMITER
first line
second line
MY_DELIMITER");

两边的分隔串是无意义的,可以取消.像q".hello."打印出来什么,很难确定.
q{},{必须是d源代码}.帮助编辑器显示d源代码
字面量是编译时计算的,为了优化.

发布了381 篇原创文章 · 获赞 25 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/fqbqrr/article/details/104587192