【问题标题】:How to use a protocol with associative type as return value in Swift?如何在 Swift 中使用具有关联类型的协议作为返回值?
【发布时间】:2015-08-08 16:21:56
【问题描述】:

我正在尝试使用具有关联类型的协议作为类 func 的返回值。

Another post here on stackoverflow 是关于同一个问题,根据这篇文章的回答,我尝试了以下方法:

// addArg takes a single Int argument and returns something, 
// which is capable of adding a another Int argument
class OpWithNoArg {
    func addArg<OP: OpWithOneArg where OP.ArgType == Int>(arg: Int) -> OP {
        return OpWithOneArgImpl() // (1) error here !
    }
}

// a protocol with associated type, which allows adding another
// argument of a not yet specified type
protocol OpWithOneArg {
    typealias ArgType
    func addAnotherArg(arg: ArgType)
}

// an implementation of the protocol above, 
// which fixes the associated type to an Int
class OpWithOneArgImpl : OpWithOneArg {
    typealias ArgType = Int
    func addAnotherArg(arg: Int) {
        // ...
    }
}

Xcode 7.0 Beta 4 中标有 (1) 的行的错误是

无法将“OpWithOneArgImpl”类型的返回表达式转换为 预期返回类型“OP”

如果我将返回值更改为可选并返回nil,则示例编译成功:

// return null  
class OpWithNoArg {
    func addArg<OP: OpWithOneArg where OP.ArgType == Int>(arg: Int) -> OP? {
        // return OpWithOneArgImpl()
        return nil
    }
}

这种使用关联类型的协议作为返回值的方式是否可以在swift中使用,如果是这种情况,如何修复上面提到的编译器错误?

提前致谢!

编辑:

在 Java 中,代码类似于以下 sn-p。我试图在 Swift 中找到一种方法来实现同样的目标。

class C {
    <T> P<T> f(T arg) {
        return new PImpl();
    }
}

interface P<S> {
    void g(S arg);
}

class PImpl<S> implements P<S> {
    PImpl() {}
    public void g(S arg) {}
}

【问题讨论】:

  • 见stackoverflow.com/a/27406808。基本上,OP 不是“OpWithOneArg 类型为Int”,而是在调用它时使用的任何结果。这种类型可能无法从OpWithOneArgImpl 转换,所以它不允许你。
  • 您的链接包含一个非常有用的解释!像我这样有大量 Java 背景的人必读;)

标签: swift generics


【解决方案1】:

不清楚您要实现什么,但恕我直言,错误源于这样一个事实,即在您的 addArg 函数中您定义了一个泛型类型 OP,它通常应在函数的参数和主体中使用。

相反,您返回一个非泛型类型,试图强制将其视为泛型 `OP。

快速修复可能是强制转换为您的返回对象的OP:

return OpWithOneArgImpl() as !OP

但我不会推荐它。

在您的示例中,您的addArg 将始终返回OpWithOneArgImpl,因此它可以定义为func addArg(arg: Int) -&gt; OpWithOneArgImpl

希望这会有所帮助。

编辑:

这可能不是您想要实现的目标,但希望它可以提供帮助 更清楚地解释我上面的意图。

protocol OpWithOneArg {
    typealias ArgType
    func addAnotherArg(arg: ArgType)
    init() // Must have a init with no argument
}


class OpWithOneArgImpl : OpWithOneArg {
    typealias ArgType = Int
    func addAnotherArg(arg: Int) {
        // ...
    }
    required init() {
        // init implementation
    }
}

class OpWithNoArg {
    func addArgWithOPType<OP: OpWithOneArg where OP.ArgType == Int>(op: OP.Type, arg: Int) -> OP {
        return OP() // Here we use the init
    }
}

let foo = OpWithNoArg()
foo.addArgWithOPType(OpWithOneArgImpl.self, arg: 3)

编辑:

也许您可能会研究泛型类型的使用:

protocol OpWithOneArg {
    typealias ArgType
    func addAnotherArg(arg: ArgType)
    init() // Must have a init with no argument
}


class OpWithOneArgImpl<T> : OpWithOneArg { // Generic implementation confirming to protocol
    typealias ArgType = T
    func addAnotherArg(arg: T) {
        // ...
    }
    required init() {
        // init implementation
    }
}


class OpWithNoArg {
    func addArgWithOPType<OP: OpWithOneArg>(op: OP.Type, arg: OP.ArgType) -> OP {
        return OP() // Here we use the init
    }
}

let foo = OpWithNoArg()

// OpWithOneArgImpl<Int>
foo.addArgWithOPType(OpWithOneArgImpl.self, arg: 3) 

// OpWithOneArgImpl<String>
foo.addArgWithOPType(OpWithOneArgImpl.self, arg: "Hello")

【讨论】:

  • 我在帖子中添加了一个 Java 代码示例,它显示了我在 Swift 中尝试做的事情:)
  • Ups ...对不起,我不精通Java :(也许其他人可以提供帮助
  • 使用强制转换可以解决编译器错误,但是,调用函数没有按预期工作:OpWithNoArg().addArg(42).addAnotherArg(35) 导致编译器错误 cannot invoke 'addAnotherArg' with a argument list of输入'(Int)'
  • 目前我的实现看起来像你的建议:func addArg(arg: Int) -&gt; OpWithOneArgImpl,但是 - 在 Java 中你总是试图引入接口 - 我试图摆脱返回一个具体的实现并返回一个协议/接口反而。但也许这不是 Swift 的方式:)
  • 您可能会将OP 视为编译器使用的类型的占位符。这解释了为什么强制转换不起作用:编译器不知道 OP 实际上应该是什么。与其他解决方案编译器在编译时知道返回什么类型。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多