关于Swift函数,命名参数和类型管理(About Swift functions, named parameters, and type management)

编程入门 行业动态 更新时间:2024-10-18 08:27:23
关于Swift函数,命名参数和类型管理(About Swift functions, named parameters, and type management)

假设我有一个重载的函数,如下所示:

func doMath(mathOption: String) -> (Int...) -> Double { ... return average } func doMath(mathOption: String) -> ([Int]) -> Double { ... return average }

附注:函数average本身被重载以接受数组作为输入或参数列表。

两个问题:

1 - 我如何参考我所指的功能?

例如:

let doAverage = doMath(mathOption: "average")

如何指定我打电话的doMath功能? 斯威夫特感到困惑,无法从下一行推断出来:

如果我后来写道:

doAverage(1,2,3,4)

2 - 如何命名参数? 原来的平均函数被这样调用:

average(nums: 1,2,3,4)

我必须命名参数。 然而,对于doAverage,我无法命名参数,因为返回类型是如何定义的。

3 - 我如何创建一个类型(可能使用struct?)来简化这个假设代码。

感谢您提供的任何帮助,解释或答案!


编辑,澄清3,这里是局势的扩展版本:

func sumAll(nums: [Int]) -> Double { return Double(nums.reduce(0, { (a,b) in a+b})) } func sumAll(nums: Int...) -> Double { return sumAll(nums: nums) } func average(nums: [Int]) -> Double { return sumAll(nums: nums) / Double(nums.count) } func average(nums: Int...) -> Double { return average(nums: nums) } func doMath(mathOption: String, nums: Int...) -> Double { if mathOption == "average" { return average(nums: nums) } else { return sumAll(nums: nums) } } typealias mathReturnType1 = (Int...) -> Double typealias mathReturnType2 = ([Int]) -> Double func doMath(mathOption: String) -> mathReturnType1 { return average } func doMath(mathOption: String) -> mathReturnType2 { return average }

我使用了typealias来创建两个示例类型。 某种类型是否可以以某种方式处理这两种情况? 对我来说,这是有道理的,如果相同的函数被重载来处理不同的输入,为什么不是类型? 也许这是一个天真的观点,或者有一种方法可以表达我在Swift中的想法?

Suppose I have a function overloaded as such:

func doMath(mathOption: String) -> (Int...) -> Double { ... return average } func doMath(mathOption: String) -> ([Int]) -> Double { ... return average }

Side note: Function average itself is overloaded to accept both an array as an input or a list of parameters.

Two questions:

1 - How do I reference which function I am referring to?

For example:

let doAverage = doMath(mathOption: "average")

How do I specify which doMath function I'm calling? Swift is confused and can't infer from the next line:

If I later write:

doAverage(1,2,3,4)

2 - How do I name parameters? The original average function is called thus:

average(nums: 1,2,3,4)

I have to name the parameters. Yet with doAverage, I can't name parameters because of how the return type is defined.

3 - How could I create a type (perhaps using struct?) to simplify this hypothetical code.

Thanks for any help, explanation, or answers you offer!


Edit, to clarify 3, here is the expanded version of the situation:

func sumAll(nums: [Int]) -> Double { return Double(nums.reduce(0, { (a,b) in a+b})) } func sumAll(nums: Int...) -> Double { return sumAll(nums: nums) } func average(nums: [Int]) -> Double { return sumAll(nums: nums) / Double(nums.count) } func average(nums: Int...) -> Double { return average(nums: nums) } func doMath(mathOption: String, nums: Int...) -> Double { if mathOption == "average" { return average(nums: nums) } else { return sumAll(nums: nums) } } typealias mathReturnType1 = (Int...) -> Double typealias mathReturnType2 = ([Int]) -> Double func doMath(mathOption: String) -> mathReturnType1 { return average } func doMath(mathOption: String) -> mathReturnType2 { return average }

I've used typealias to create two example types. Could a type be overloaded somehow to handle both situations? To me, this makes sense in that if the same function is being overloaded to handle different inputs, why not the type? Perhaps this is a naive perspective or perhaps there is a way to express what I'm thinking in Swift?

最满意答案

如何引用该功能? 只需指定类型!

func doMath(mathOption: String) -> (Int...) -> Double { return { (values: Int...) -> Double in return Double(values.reduce(0, +)) / Double(values.count) } } func doMath(mathOption: String) -> ([Int]) -> Double { return { (values: [Int]) -> Double in return Double(values.reduce(0, +)) / Double(values.count) } }

let average1 = doMath(mathOption: "x") as (Int...) -> Double print(average1(1, 2, 3))

要么

let average1: (Int...) -> Double = doMath(mathOption: "x") print(average1(1, 2, 3))

我也建议使用typealias命名该类型。

你的第二个问题 - 你不能在函数类型中命名参数。

How to reference the function? Just specify the type!

func doMath(mathOption: String) -> (Int...) -> Double { return { (values: Int...) -> Double in return Double(values.reduce(0, +)) / Double(values.count) } } func doMath(mathOption: String) -> ([Int]) -> Double { return { (values: [Int]) -> Double in return Double(values.reduce(0, +)) / Double(values.count) } }

let average1 = doMath(mathOption: "x") as (Int...) -> Double print(average1(1, 2, 3))

or

let average1: (Int...) -> Double = doMath(mathOption: "x") print(average1(1, 2, 3))

I would also advise to to name that type using a typealias.

Your second question - you cannot name parameters in function types.

更多推荐

本文发布于:2023-04-28 06:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1330326.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   参数   类型   Swift   functions

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!