Swift协议继承&通用功能 [英] Swift Protocol Inheritance & Generic functions

查看:128
本文介绍了Swift协议继承&通用功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下游乐场:

import Foundation

protocol StringInitable {
    init( string:String )
}

class A : StringInitable {
    var stored:String

    required init ( string:String ) {
        stored = string
    }
}

class B : A /*, StringInitable */ {
    var another_stored:String

    required init ( string:String ) {
        another_stored = "B-store"

        super.init(string: string)
    }
}

func maker<T:StringInitable>(string:String) -> T {
    return T(string: string)
}

let instanceA = A(string: "test-maker-A")
let instanceB = B(string: "test-maker-B")

let makerA:A = maker("test-maker-A")
let makerB:B = maker("test-maker-B")

let typeInstanceA = _stdlib_getTypeName(instanceA)
let typeMakerA = _stdlib_getTypeName(makerA)

let typeInstanceB = _stdlib_getTypeName(instanceB)
let typeMakerB = _stdlib_getTypeName(makerB)

从结果中,编译器似乎已推断出正确的类型但未能调用正确的初始化器。为什么我必须在B类中显式实现StringInitable(通过删除B类定义中的注释来测试)以使泛型函数maker调用正确的初始值设定项?

From the results the compiler seems to have inferred the correct types but has failed to call the correct initializers. How come I have to explicitly implement StringInitable in the B-class (test by removing the comment in the B class definition) to have the generic function "maker" call the correct initializer?

推荐答案

这有点像编译器错误,原因很简单: makerB B的变量 type,但它被分配了一个 A 的实例。这应该是不可能的,事实上如果你试图打印,并且更一般地来说是访问 makerB的 another_stored 属性变量,引发运行时异常,我不会期待别的。

That smells like a compiler bug for one simple reason: makerB is a variable of B type, but it is assigned an instance of A. This should not be possible, and in fact if you try to print, and more generally to access to, the another_stored property of the makerB variable, a runtime exception is raised, and I wouldn't expecting nothing else.

这是因为如果 B A 的子类, A 的实例无法分配给<$ c $的变量c> B 类型(反之亦然)。

That's because if B is a subclass of A, an instance of A cannot be assigned to a variable of B type (whereas the opposite is possible).

将变量 A 类型分配给变量 B 类型是可能的,但仅在这些条件下:

Assigning a variable of A type to a variable of B type is possible though, but only under these conditions:


  • 来自 A B 已完成(否则编译器应该出错)

  • A引用的实例变量实际上是 B 的实例(否则应该引发运行时异常)

  • an explicit downcast from A to B is done (the compiler should error otherwise)
  • the instance referenced by the A variable is actually an instance of B (a runtime exception should be raised otherwise)

请注意,编译器不仅无法调用正确的初始化程序 - 它调用了另一个类的初始化程序

Note that the compiler didn't just fail to call the correct initializer - it called the initializer of another class

这篇关于Swift协议继承&amp;通用功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆