在快速,可以一个函数是一个类型? [英] In swift, can a function be a type?

查看:108
本文介绍了在快速,可以一个函数是一个类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在通过按下command + click来研究XCTAssert方法时,它看起来像是基础方法是一个具有类型(符合Equatable协议的通用类型,称为T)的函数。我是否正确地说,如果是的话功能如何符合协议?函数类型是什么?

  public func XCTAssertEqual< T:Equatable>(_ expression1:@autoclosure()throws  - > ArraySlice< T>,_ expression2:@autoclosure()throws  - > ArraySlice< T> _ message:@autoclosure() - > String = default,file:StaticString = #file,line:UInt = #line)

这条线是我想要解释的最令人困惑的:

  func XCTAssertEqual >`


  typealias FooType =(Int,String) - > String 

func foo(i:Int ,s:String) - > String {
return s +_\(i)
}

func bar(foo0:FooType) - > String {
return foo0(100,alpha)
}

print(bar(foo))// alpha_100

let f:FooType = { i,s in
return s +_\(i)
}

print(f(200,beta))// beta_200

一个附录,尤其适用于布兰登: - )

<$ p $在$ far $ {
print $ {
} (f())
/ *
1
2
3
* /
}
pre>

附录2,用于Brandon

  func f1() - > Int {
return 1
}
func f2() - > Int {
return 2
}

let farr2: [() - > Int] = [f1,f2]
for farr2 {
print(f())
/ *
1
2
* /
}

应用程序3: - )

  let farr2 = [f1,f2 ] 
for farr2 {
print(f())
/ *
1
2
* /
}

来自apple文档


Swift中的每个函数都有一个类型,由函数的
参数类型和返回类型组成。您可以像Swift中的任何其他
类型一样使用此类型,这样可以轻松地将函数作为参数传递给
其他函数,并从函数返回函数。函数
也可以写入其他函数中,以封装嵌套函数范围内的有用
功能。



While researching XCTAssert methods by pressing command+click, it looks like they underlying method is a function that has a type (a generic type referred to as T, that conforms to the Equatable protocol). Am I saying this correctly, and if so how do functions comform to protocols? Are functions types?

public func XCTAssertEqual<T : Equatable>(_ expression1: @autoclosure () throws -> ArraySlice<T>, _ expression2: @autoclosure () throws -> ArraySlice<T>, _ message: @autoclosure () -> String = default, file: StaticString = #file, line: UInt = #line)

This line is the most confusing which I'm trying to explain above:

func XCTAssertEqual<T : Equatable>`

解决方案

Every function has a specific function type, made up of the parameter types and the return type of the function.

typealias FooType = (Int, String)->String

func foo(i: Int, s: String)->String {
    return s + "_\(i)"
}

func bar(foo0: FooType)->String {
    return foo0(100, "alpha")
}

print(bar(foo))         // alpha_100

let f:FooType = { i, s in
    return s + "_\(i)"
}

print(f(200, "beta"))   // beta_200

an appendix, especially for Brandon :-)

let farr:[()->Int] = [{return 1}, {return 2}, {return 3}]
for f in farr {
    print(f())
/*
 1
 2
 3
*/
}

appendix 2, for Brandon

func f1()->Int {
    return 1
}
func f2()->Int {
    return 2
}

let farr2:[()->Int] = [f1, f2]
for f in farr2 {
    print(f())
/*
 1
 2
 */
}

app 3 :-)

let farr2 = [f1, f2]
for f in farr2 {
    print(f())
/*
 1
 2
 */
}

from apple docs

Every function in Swift has a type, consisting of the function’s parameter types and return type. You can use this type like any other type in Swift, which makes it easy to pass functions as parameters to other functions, and to return functions from functions. Functions can also be written within other functions to encapsulate useful functionality within a nested function scope.

这篇关于在快速,可以一个函数是一个类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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