参考自d程序设计语言---个人博客http://my.oschina.net/u/218155/blog?fromerr=SwOkb7Sw fllow mejava
变量是语言最基础的部分,咱们经过变量去表示数据。代码最最底层的都是这些基础的数据类型。
数组
D语言提供了整型,浮点型,字符型变量。函数
d语言能够让你像脚本语言同样自由的使用变量。ui
使用auto关键字spa
例如 auto a = 1.0; auto f = 1;
D语言也定义了string类型,让你轻松的处理字符串 string在D中其实是immutable(char)[]类型.net
单个字符是不能够改变.immutable不可变的。设计
string s = "abc"; s[0] = 'd';//error s = "adfad"; //ok
d语言也提供其余的字符类型code
wstring wsx = "helolow oorld"; dstring dsx = "hellow aadfads";
获取类型的标识方法typeoform
typeid(typeof(ss1))
咱们也能够经过typeof定义新的变量blog
typeof(s) a = "ddd";
数组的定义.在D中固定长度的数组和动态数组稍有不一样
固定长度的数组 int arr[3] = [1,2,3]; 动态数组:auto arr[] = [1,2,3];
关联数组定义
int[string] myarr2 = ["pi":12,"kk":33,"son":123]; 或auto myarr2 = ["pi":12,"kk":33,"son":123];
D中函数的定义
auto fff = function double(int x){return x/10.;}; 或者double function(int) fff2 = function double(int x){return x/10.;};
D中若是函数想运用外部变量使用delegate代替function,delegate效率比function低,由于他得维持一堆信息
auto ffc = 2; auto fff3 = delegate double(int x) {return ffc*x/10.;};
D中用is作类型判断.is包括以下四种状况
1 判断类型是否存在 is(int[]); 2 判断两个类型是否相等 is(int == uint); 3 判断int可否转换为unit is(int : uint)); 4 判断一些内部的类型好比方法,struct is(typeof(f2) == function)
我写的简单例子
import std.stdio; void main() { auto a = 1; auto b = 1E2f; writeln(a,b); string ss = "hi"; string ss1 = r"ss1"; auto ss2 = x"0A"; writeln(ss,ss1,ss2); int arr[2] = [1,2]; int[string] arr2 = ["a":1,"b":2]; writeln(typeid(typeof(ss1)),ss1.length,arr,arr2); auto f = function double(int x){return x/10;}; double function(int x) f2 = function double(int x){return x/10;}; writeln(f(11),f2(11)); auto f3 = delegate double(int x){return (x+a)/10;}; writeln(f3(9)); assert(1==1); string ss11 = "a123 = 21;"; ss11 ~= ""; //mixin(ss11); writeln(a,ss11); writeln(is(int[]),is(add),is(f2)); writeln(is(int == uint),is(int : uint)); writeln(is(typeof(f2) == function)); auto arr21 = new int[4]; assert(arr21 == [0,0,0,0]); assert(arr21.length == 4); writeln(typeid(typeof(a))); auto cfff = cast(float) a; writeln(typeid(typeof(a)),typeid(typeof(cfff))); writeln("add" in arr2); }