gpt4 book ai didi

function - Swift、数学函数和协议(protocol)

转载 作者:搜寻专家 更新时间:2023-10-31 08:27:49 26 4
gpt4 key购买 nike

我正在尝试创建 2 个协议(protocol) ArithmeticType 和 MathematicType,它们将在通用运算符函数的 where 子句中使用

protocol ArithmeticType {
func +(lhs: Self, rhs: Self) -> Self
func -(lhs: Self, rhs: Self) -> Self
func *(lhs: Self, rhs: Self) -> Self
func /(lhs: Self, rhs: Self) -> Self
}

extension Int : ArithmeticType {
}

extension Double : ArithmeticType {
}

extension Float : ArithmeticType {
}

ArithmeticType 按预期工作,Int、Float 和 Double 都符合它。但是以下失败了

import Darwin

protocol MathematicType {
func sin(x: Self) -> Self
}

extension Double : MathematicType {
}

extension Float : MathematicType {
}

在 Playground 的控制台输出上,我读到:

Playground execution failed: <EXPR>:35:1: error: type 'Double' does not conform to protocol 'MathematicType'
extension Double : MathematicType {
^
<EXPR>:32:10: note: protocol requires function 'sin' with type 'Double -> Self'
func sin(x: Self) -> Self
^
<EXPR>:39:1: error: type 'Float' does not conform to protocol 'MathematicType'
extension Float : MathematicType {
^
<EXPR>:32:10: note: protocol requires function 'sin' with type 'Float -> Self'
func sin(x: Self) -> Self
^

我希望数学函数的行为类似于上面的运算符。有什么办法吗?

== 编辑:

现在我意识到试图简化我的问题是个坏主意。上下文是这个类(可选值的向量)

class Vector<T> {

var data=[T?]()

init(fromArray: Array<T>) {
for i in fromArray {
data.append(i)
}
}

init() {
}

init(count: Int){
for i in 0..<count {
data.append(nil)
}
}

init(count: Int, repeatedValue: T) {
for i in 0..<count {
data.append(repeatedValue)
}
}

func count() -> Int {
return data.count
}

func append(newElement: T?) {
data.append(newElement)
}

subscript(index: Int) -> T? {
let i = index>0 ? index % count() : -index % count()
return data[i]
}
}

在它之外,我为 + 运算符定义了一个通用函数

func +<T where T: ArithmeticType>(left: Vector<T>, right: Vector<T>) -> Vector<T> {
let resultCount = max(left.count(),right.count())
var result = Vector<T>()
for i in 0..<resultCount {
if left[i] != nil && right[i] != nil {
result.append(left[i]!+right[i]!)
}
else {
result.append(nil)
}
}
return result
}

按预期工作,但是当我尝试将通用 sin 函数定义为

func sin<T where T : FloatingPointType>(x: Vector<T>) -> Vector<T>{
var result = Vector<T>()
for i in 0..<x.count() {
if let o = x[i] {
result.append(sin(o))
}
else {
result.append(nil)
}
}
return result
}

我得到“找不到接受所提供参数的 sin 重载”

然后我尝试使用 MathemticType 来模仿我已经为 + 运算符所做的事情

(ArithmeticType 的灵感来自 IntegerAritmeticType 源代码,通过单击 import swift 的命令找到的比我对我在做什么的了解更多)

==更新

如果我只为 Double 编写专门的函数

func sin(x: Vector<Double>) -> Vector<Double>{
var result = Vector<Double>()
for i in 0..<x.count() {
if let o = x[i] {
result.append(Darwin.sin(o))
}
else {
result.append(nil)
}
}
return result
}

它按预期工作。

所以问题可能变成“我如何将其概括为 Double 和 Float”?

最佳答案

编译错误是因为你将sin()声明为MathematicType协议(protocol)的一个方法,然后声明Double实现了MathematicType,但实际上并没有写sin()方法。

extension Double {
func sin(x: Double) -> Double {
return Darwin.sin(x)
}
}

不过,我认为这不是您想要的,对吗?你希望能够这样写:

let myAngle = 3.14159
let sineValue = myAngle.sin()

如果是这种情况,您的协议(protocol)和扩展需要如下所示:

protocol MathematicType {
func sin() -> Self
}

extension Double : MathematicType {
func sin() -> Double {
return Darwin.sin(self)
}
}

关于function - Swift、数学函数和协议(protocol),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25487274/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com