简体   繁体   English

如何在typealias中使用类型参数?

[英]How to use type parameter in typealias?

I want to use a typealias for a function which is used in a generic class. 我想为泛型类中使用的函数使用类型别名。 One of the parameters of the function uses a generic type of the class. 函数的参数之一使用类的泛型类型。 This is a simplified example: 这是一个简化的示例:

class Bar {}

// no typealias - works
class TestGeneric<T: Bar> {
    let myFunc: (T) -> String

    init(myFunc: (T) -> String) {
        self.myFunc = myFunc
    }
}

let myFunc: (Bar) -> String = {(bar: Bar) in
    return "hello"
}

let test = TestGeneric<Bar>(myFunc: myFunc)
println(test.myFunc(Bar()))

// typealias - doesn't compile
class TestTypealias<T: Bar> {
    typealias MyFuncGeneric = (T) -> String

    let myFunc: MyFuncGeneric

    init(myFunc: MyFuncGeneric) {
        self.myFunc = myFunc
    }
}

let myFunc2: TestTypealias.MyFuncGeneric = {(bar: Bar) in
    return "hello"
}

let test2 = TestTypealias<Bar>(myFunc: myFunc2) // Error: Cannot invoke initializer for type 'TestTypealias<Bar>' with an argument list of type '(myFunc: T -> String)'
println(test2.myFunc(Bar()))

Is there a way to solve this? 有办法解决吗? Or do I have to forgo typealias when using generics? 还是在使用泛型时必须放弃typealias? In the actual code I have a lot of parameters and really need a typealias... 在实际的代码中,我有很多参数,确实需要一个类型别名。

(Swift 1.2) (快速1.2)

This compiles and behaves as expected: 这将编译并按预期方式运行:

class Test< T >
{
    typealias GenFunc = ( thing: T ) -> Void

    let thing : T

    init( thing: T )
    {
        self.thing = thing
    }

    func ExecuteFunc( genFunc: GenFunc )
    {
        genFunc( thing: self.thing )
    }
}

Use: 采用:

let t = Test( thing: "blah" )

t.ExecuteFunc()
{ ( thing: String ) -> Void in
    println( thing )
}

By writing 通过写

class TestGeneric<T: Bar> 

all you've really done is to define T universally as being of type Bar when used inside of a method, so you can write this: 您真正要做的就是将T定义为在方法内部使用时的Bar类型,因此您可以这样编写:

func myFunc(aValue:T) {

}

instead of this: 代替这个:

func myFunc<T: Bar>(aValue:T) {

    }

each time you want to define T as being of type Bar. 每次您想将T定义为Bar类型。

If you want the class itself to be generic you use only <T> , for example: class TestGeneric<T> and type is defined upon initializing. 如果希望类本身是泛型的,则仅使用<T> ,例如: class TestGeneric<T>并且类型在初始化时定义。

class Bar {}

// no typealias - works
class TestGeneric<T> {
    let myFunc: (T) -> String

    init(myFunc: (T) -> String) {
        self.myFunc = myFunc
    }
}

let myFunc: Bar -> String = {(bar: Bar) in
    return "hello"
}

let test = TestGeneric<Bar>(myFunc: myFunc)
println(test.myFunc(Bar()))

// typealias - doesn't compile
class TestTypealias<T> {
    typealias MyFuncGeneric = T -> String
    func myFunc(aValue:T) {

    }
    let myFunc: MyFuncGeneric

    init(myFunc: MyFuncGeneric) {

        self.myFunc = myFunc
    }
}

let myFunc2: TestTypealias<Bar>.MyFuncGeneric = {(bar: Bar) in
    return "hello"
}

let test2 = TestTypealias<Bar>(myFunc: myFunc2)   
println(test2.myFunc(Bar()))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM