聲明
聲明: typedef Decl alias Decl Decl Decl: 存儲類別 Decl 基本類型 多個聲明符 ; 基本類型 聲明符 函數體 多個聲明符: 聲明符初始值 聲明符初始值 , 聲明符初始值列表 聲明符初始值: 聲明符 聲明符 = 初始值 聲明符標誌符列表: 聲明符標誌符 聲明符標誌符 , 聲明符標誌符列表 聲明符標誌符: 標誌符 標誌符 = 初始值 基本類型: bit byte ubyte short ushort int uint long ulong char wchar dchar float double real ifloat idouble ireal cfloat cdouble creal void .標誌符列表 標誌符列表 Typeof Typeof . 標誌符列表 基本類型2: * [ ] [ 表達式 ] [ 類型 ] delegate ( 參數列表 ) function ( 參數列表 ) 聲明符: 基本類型2 聲明符 標誌符 ( 聲明符 ) 標誌符 多個聲明符後綴 ( 聲明符 ) 多個聲明符後綴 多個聲明符後綴: 聲明符後綴 聲明符後綴 多個聲明符後綴 聲明符後綴: [ ] [ 表達式 ] [ 類型 ] ( 參數列表 ) 標誌符列表: 標誌符 標誌符 . 標誌符列表 模板實例 模板實例 . 標誌符列表 Typeof: typeof ( 表達式 ) 存儲類別: abstract auto const deprecated final override static synchronized 類型: 基本類型 基本類型 聲明符2 聲明符2: 基本類型2 聲明符2 ( 聲明符2 ) ( 聲明符2 ) 聲明符後綴 參數列表: 參數 參數 , 參數列表 ... 參數: 聲明符 聲明符 = 賦值表達式 InOut 聲明符 InOut 聲明符 = 賦值表達式 InOut: in out inout
Declaration: typedef Decl alias Decl Decl Decl: StorageClass Decl BasicType Declarators ; BasicType Declarator FunctionBody Declarators: DeclaratorInitializer DeclaratorInitializer , DeclaratorIdentifierList DeclaratorInitializer: Declarator Declarator = Initializer DeclaratorIdentifierList: DeclaratorIdentifier DeclaratorIdentifier , DeclaratorIdentifierList DeclaratorIdentifier: Identifier Identifier = Initializer BasicType: bit byte ubyte short ushort int uint long ulong char wchar dchar float double real ifloat idouble ireal cfloat cdouble creal void .IdentifierList IdentifierList Typeof Typeof . IdentifierList BasicType2: * [ ] [ Expression ] [ Type ] delegate ( ParameterList ) function ( ParameterList ) Declarator: BasicType2 Declarator Identifier ( Declarator ) Identifier DeclaratorSuffixes ( Declarator ) DeclaratorSuffixes DeclaratorSuffixes: DeclaratorSuffix DeclaratorSuffix DeclaratorSuffixes DeclaratorSuffix: [ ] [ Expression ] [ Type ] ( ParameterList ) IdentifierList Identifier Identifier . IdentifierList TemplateInstance TemplateInstance . IdentifierList Typeof typeof ( Expression ) StorageClass: abstract auto const deprecated final override static synchronized Type: BasicType BasicType Declarator2 Declarator2: BasicType2 Declarator2 ( Declarator2 ) ( Declarator2 ) DeclaratorSuffixes ParameterList: Parameter Paremeter , ParameterList ... Parameter: Declarator Declarator = AssignExpression InOut Declarator InOut Declarator = AssignExpression InOut: in out inout
聲明語法
聲明語法通常從右向左讀:
x; * x; ** x; [] x; *[] x; []* x;
數組,從左向右讀(列優先):
[3] x; [3][5] x; [3]*[5] x;
指向函數的指針用關鍵字 function 聲明:
function() x; function()[] x;
也可以使用 C 風格的數組聲明方式(行優先):
x[3]; x[3][5]; (*x[5])[3]; (*x)(); (*[] x)();
在多重聲明中,所有被聲明的符號擁有相同的類型:
x,y; * x,y; x,*y; [] x,y; x[],y;
類型定義
可以通過 typedef 引入強類型。對於函數重載和調試器來說,在語義上,強類型是類型檢查系統可以將其同其他類型區分的類型。myint; foo( x) { . } foo(myint m) { . } . myint b; foo(b);
Typedef 可以指定一個不同於其底層類型(underlying type)的初始值:
myint = 7; myint m;
類型別名
有時爲類型起一個別名是很方便的,例如可以作爲一個冗長複雜類型(比如一個函數指針)的簡寫形式。在 D 中,可以使用別名聲明達到這個目的:alias abc.Foo.bar myint;
別名類型在語義上等價於原類型。調試器無法區分它們,在函數重載是也不會區分它們。例如:
alias myint; foo( x) { . } foo(myint m) { . }
類型別名等價於 C 中的 typedef 。
別名聲明
符號可以被聲明爲另一個符號的 別名 。例如:
string; alias string.strlen mylen; ... len = mylen();
下面的別名聲明是合法的:
Foo2(T) { alias T t; } alias Foo2!() t1; alias Foo2!().t t2; alias t1.t t3; alias t2 t4; t1.t v1; t2 v2; t3 v3; t4 v4;
別名符號可以將一個較長的符號簡記爲一個較短的符號,還可以將一個引用從一個符號重定位到另一個符號:
(Win32) { alias win32.foo myfoo; } (linux) { alias linux.bar myfoo; }
別名可以用來從模塊中‘導入’一個符號到當前作用域:
alias string.strlen strlen;
別名還可以‘導入’一系列的重載函數,這樣就可以在當前作用域中重載函數:
A { foo( a) { 1; } } B : A { foo( a, b ) { 2; } } C : B { foo( a ) { 3; } alias B.foo foo; } D : C { } test() { D b = D(); i; i = b.foo(1, 2u); i = b.foo(1); }
注意:類型別名有時會同別名聲明十分相似:
alias foo.bar abc;
typeof
Typeof 用來獲得一個表達式的類型。例如:
func( i) { typeof(i) j; typeof(3 + 6.0) x; typeof(1)* p; [typeof[p]] a; printf(, typeof().size); c = (typeof(1.0))j; }
Expression 不會被計算,只會生成它的類型:
func() { i = 1; typeof(++i) j; printf(, i); }
有兩種特殊情況:就算不在成員函數中,typeof(this) 也將生成 this 在非靜態成員函數中的類型。類似的,typeof(super) 也將生成 super 在非靜態成員函數中的類型。
A { } B : A { typeof(this) x; typeof(super) y; } C { typeof(this) z; typeof(super) q; } typeof(this) r;
在編寫模板代碼時 Typeof 大有用武之地。