DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> JavaScript入門知識 >> AJAX入門 >> AJAX詳解
D 語言的混入(Mixin):在編譯期把程式碼「剪貼」到呼叫處
編輯:AJAX詳解     

「Mixin」這個詞在不同語言裡指的不是同一件事。在 D 語言中,mixin 指的是:從一個範本宣告的程序體中取出一整組宣告,插入到目前所在的上下文裡。理解這句話,就理解了 D 的 mixin。

語法

模板混入:
  mixin 模板標識符 ;
  mixin 模板標識符 混入標識符 ;
  mixin 模板標識符 !( 模板參數列表 ) ;
  mixin 模板標識符 !( 模板參數列表 ) 混入標識符 ;

混入標識符: 標識符

模板混入可以出現在模組、類別、結構、union 以及陳述句的宣告清單中。模板標識符指向一個範本宣告;若該範本沒有參數,就可以省略 !(...) 的部分。

與範本具現化的關鍵差異

範本具現化在定義範本的地方求值,而模板混入的程序體是在混入所在的作用域內求值。效果等同於用剪貼簿把範本內容貼到混入的位置。這讓兩件事變得可能:注入參數化的樣板程式碼,以及建立具現化無法處理的巢狀函式。

template Foo() { int x = 5; }
mixin Foo;

struct Bar { mixin Foo; }

void test() {
    printf("x = %d\n", x);   // 印出 5
    {
        Bar b;
        int x = 3;
        printf("b.x = %d\n", b.x);  // 印出 5
        printf("x = %d\n", x);      // 印出 3
        {
            mixin Foo;
            printf("x = %d\n", x);  // 印出 5
            x = 4;
            printf("x = %d\n", x);  // 印出 4
        }
        printf("x = %d\n", x);      // 印出 3
    }
    printf("x = %d\n", x);          // 印出 5
}

參數化與虛擬函式注入

混入可以帶型別參數:

template Foo(T) { T x = 5; }
mixin Foo!(int);   // 建立型別為 int 的 x

也可以為類別補上虛擬函式,讓子類別正常覆寫:

template Foo() {
    void func() { printf("Foo.func()\n"); }
}
class Bar  { mixin Foo; }
class Code : Bar {
    void func() { printf("Code.func()\n"); }
}

void test() {
    Bar b = new Bar();
    b.func();          // 呼叫 Foo.func()
    b = new Code();
    b.func();          // 呼叫 Code.func()
}

作用域解析:用外層變數而非全域變數

int y = 3;
template Foo() { int abc() { return y; } }

void test() {
    int y = 8;
    mixin Foo;         // 使用的是區域的 y
    assert(abc() == 8);
}

别名參數同樣可用來參數化符號本身:

template Foo(alias b) { int abc() { return b; } }

void test() {
    int y = 8;
    mixin Foo!(y);
    assert(abc() == 8);
}

實戰範例:泛型 Duff's Device

下面這個例子用混入為任意陳述句實作了 Duff's Device,被傳入的陳述句以粗體表示。混入在展開的同時也建立了巢狀函式與委派字面值,編譯器可以將它內聯掉:

template duffs_device(alias id1, alias id2, alias s) {
    void duff_loop() {
        if (id1 < id2) {
            typeof(id1) n = (id2 - id1 + 7) / 8;
            switch ((id2 - id1) % 8) {
                case 0: do { s();
                case 7:      s();
                case 6:      s();
                case 5:      s();
                case 4:      s();
                case 3:      s();
                case 2:      s();
                case 1:      s();
                } while (--n > 0);
            }
        }
    }
}

void foo() { printf("foo\n"); }

void test() {
    int i = 1;
    int j = 11;
    mixin duffs_device!(i, j, delegate { foo(); });
    duff_loop();       // 執行 foo() 10 次
}

混入作用域的優先規則

混入中的宣告會被「導入」外圍作用域。若名稱衝突,外圍的宣告會覆蓋混入的宣告:

int x = 3;
template Foo() { int x = 5; int y = 5; }
mixin Foo;
int y = 3;

void test() {
    printf("x = %d\n", x);   // 3
    printf("y = %d\n", y);   // 3
}

但如果兩個不同的混入在同一作用域定義了同名宣告,就會產生歧義錯誤:

template Foo() { int x = 5; }
template Bar() { int x = 4; }

mixin Foo;
mixin Bar;

void test() {
    printf("x = %d\n", x);   // 錯誤:x 模稜兩可
}

解法是加上混入標識符,用限定名稱存取:

int x = 6;
template Foo() { int x = 5; int y = 7; }
template Bar() { int x = 4; }

mixin Foo F;
mixin Bar B;

void test() {
    printf("y = %d\n", y);    // 7
    printf("x = %d\n", x);    // 6
    printf("F.x = %d\n", F.x); // 5
    printf("B.x = %d\n", B.x); // 4
}

最後要注意:混入保有自己的作用域,即使外部宣告遮蔽了同名變數,混入內部的函式看到的仍然是混入自己的那份:

int x = 4;
template Foo() {
    int x = 5;
    int bar() { return x; }
}
mixin Foo;

void test() {
    printf("x = %d\n", x);        // 4
    printf("bar() = %d\n", bar()); // 5
}

這條規則讓 mixin 具備了某種「模組化隔離」的能力,是 D 語言在編譯期 metaprogramming 上最有特色的設計之一。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved