不允许部分应用协议方法 [英] Partial application of protocol method is not allowed

查看:332
本文介绍了不允许部分应用协议方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释这个错误,以及为什么这对闭包有效?

Can someone explain this error and why this works with closure?

如果你在'B'类中将'Test'改为'A'

If you change 'Test' to 'A' inside 'B' class everything works in both cases.

beta 7

protocol Test {
    func someFunc() -> String
    var someClosure: () -> Int { get }
}

class A: Test {
    func someFunc() -> String {
        return "A String"
    }

    var someClosure: () -> Int {
        return {
            return 2
        }
    }
}

class B {
    let a: Test
    let aString: () -> String
    let aInt: () -> Int

    init(a: Test){
        self.a = a

        aString = a.someFunc // Error: Partial application of protocol method is not allowed
        aInt = a.someClosure // Works fine
    }
}

UPDATE

这里也是我古怪的分段错误集合 https://gist.github.com/aleksgapp/795a2d428008bdfa4823

Also here is my weird segmentation fault collection https://gist.github.com/aleksgapp/795a2d428008bdfa4823

如果您有任何疑问,请随时评论。 p>

Do not hesitate to comment if you have some thoughts about any.

推荐答案

更新(感谢Laszlo Korte)

UPDATE (thanks to Laszlo Korte)

从Xcode 7 Beta 2与Swift 2.0版本注释:不变异的方法结构,枚举和协议的
现在可以部分应用到他们的
self参数。

From Xcode 7 Beta 2 with Swift 2.0 Release Notes: Non-mutating methods of structs, enums, and protocols may now be partially applied to their "self" parameter.

例如:

let a: Set<Int> = [1, 2] 
let b: [Set<Int>] = [[1], [3]]

b.map(a.union) // [[1, 2], [1, 2, 3]]

ORIGINAL ANSWER (使用Swift 1.2修正Xcode 6)

ORIGINAL ANSWER (correct for Xcode 6 with Swift 1.2)

协议可以按类,结构枚举采用。在最后两种情况下,不允许部分应用结构或枚举方法,并且您得到部分应用协议方法不允许,因为 a:Test 可以是结构或枚举。

Protocol can be adopted by class, structure or enumeration. In last two cases partial application of structure or enumeration method is not allowed and you get "Partial application of protocol method is not allowed" because a: Test can be structure or enumeration.

部分应用的方法或函数换句话说 curried方法或函数。因此,当你写 a.someFunc 时,你试图部分地应用这个方法,即获得对隐式引用 a 。但是结构和枚举不是引用类型,它们是值类型,并且没有引用 a

Partially applied method or function is in other words curried method or function. So when you write a.someFunc you try to partially apply this method, i.e. get reference to closure that implicitly holds reference to a. But structures and enumerations are not reference types, they are value types and there are no reference to a.

这篇关于不允许部分应用协议方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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