<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                雖然變異也在其它地方意義重大,但是它特別經常和類型參數一起出現,并像一個驚喜。此外,非常容易觸發變異錯誤: > While variance is also relevant in other places, it occurs particularly often with type parameters and comes as a surprise in this context. Additionally, it is very easy to trigger variance errors: ~~~ class Base { public function new() { } } class Child extends Base { } class Main { public static function main () { var children = [new Child()]; // Array<Child> should be Array<Base> // Type parameters are invariant // Child should be Base var bases:Array<Base> = children; } } ~~~ 顯然,一個 Array<Child> 不能被分配到一個 Array<Base>,即使 Child可以被分配到Base。原因可能比較意外:因為 array 可以被寫入,如通過它們的 push() 方法。忽略變異錯誤非常容易產生問題: > Apparently,an `Array<Child>` cannot be assigned to an `Array<Base>`,even though `Child` can be assigned to `Base`. The reason for this might be somewhat unexpected: It is not allowed because arrays can be written to, e.g. via their `push()` method. It is easy to generate problems by ignoring variance errors: ~~~ class Base { public function new() { } } class Child extends Base { } class OtherChild extends Base { } class Main { public static function main () { var children = [new Child()]; // subvert type checker var bases:Array<Base> = cast children; bases.push(new OtherChild()); for(child in children) { trace(child); } } } ~~~ 這里我們使用 cast(第5.23節)破壞了類型檢查,因此允許了注釋行后的賦值。我們保存一個引用 bases 到原始的數組,類型為 Array<Base>。這使的推送另一個兼容Base的類型(OtherChild)到了數組。然而,我們原始的引用 children 仍然是 Array<Child>類型,當我們在迭代它的一個元素的時候遇到 OtherChild實例就會出現問題。 > Here we subvert the type checker by using a cast (5.23), thus allowing the assignment after the commented line. With that we hold a reference `bases` to the original array, typed as `Array<Base>`. This allows pushing another type compatible with `Base` (OtherChild) onto that array. However,our original reference `children` is still of type `Array<Child>` and things go bad when we encounter the `OtherChild` instance in one of its elements while iterating. 如果 Array 沒有 push() 方法,沒有其它修改的手段,賦值則變得安全,因為沒有矛盾的類型被添加到它。在Haxe中,我們可以使用結構子類型化(第3.5.2節)相應的限制類型來實現這個。 > If Array had no `push()` method and no other means of modi?cation,the assignment would be safe because no incompatible type could be added to it. In Haxe, we can achieve this by restricting the type accordingly using structural subtyping (3.5.2): ~~~ class Base { public function new() { } } class Child extends Base { } typedef MyArray<T> = { public function pop():T; } class Main { public static function main () { var a = [new Child()]; var b:MyArray<Base> = a; } } ~~~ 我們可以安全的分配 b 作為 MyArray<Base>類型,MyArray 只有一個 pop() 方法。沒有為 MyArray定義可以用來添加沖突類型的方法,因此被認為是協變的。 > We can safely assign with `b` being typed as `MyArray<Base>` and `MyArray` only having a `pop()` method. There is no method de?ned on `MyArray` which could be used to add incompatible types, it is thus said to be covariant. **協變** >[warning] 定義:協變 一種復合類型,如果它的組成類型可以被分配為缺少特定組件,如它們是只讀,不允許寫時,則被認為是協變的。 >[warning] De?nition: Covariance A compound type is considered covariant if its component types can be assigned to less speci?c components, i.e. if they are only read, but never written. >[warning] 定義:抗變性 一個復合類型,如果它的組件類型可以被分配得為缺少通用的組件,如它們只寫,但是不讀,則被認為是抗變。 >[warning] De?nition: Contravariance A compound type is considered contravariant if its component types can be assigned to less generic components, i.e. if they are only written, but never read.
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看