swift - Iterate over functions in Swift
问题描述
I am feeling a bit stupid... I want to get rid of code duplication and so I need to iterate over some functions (a couple of Darwin geometric ones). But when I try
for function in [Darwin.acos, Darwin.cos, Darwin.sin] { print(function(1.0)) }
I get the error "Ambiguous use of 'acos'". I also tried Darwin.acos(Double)
and got "Ambiguous reference to member 'acos'" and Darwin.acos { $0 }
(and similar) without any luck.
Is it possible to do this? Thank you!
解决方案
除了苏丹所说的:
在 Swift 的未来版本中,这些函数也可用作浮点类型的静态函数,比较SE-0246 Generic Math(s) Functions:
let functions = [Double.acos, Double.cos, Double.sin]
for function in functions { print(function(1.0)) }
您现在可以使用来自https://swift.org/download/#snapshots的当前 Swift 开发人员快照尝试此操作。
推荐阅读
- sql - 查询以在合格日期之后返回记录
- javascript - 如何将文本更改为选择选项文本的末尾?
- r - R/Shiny:Plotly 中线条的独特颜色
- sql - SQL 查询中的 ANY 和 ALL 运算符
- java - 无法弄清楚为什么矩阵 a 双打
- pine-script - pinescript 尾随停止在第一个之后不起作用
- docker - 为基于 WSL 的 ubuntu 20.04 设置 docker 时出现问题。我想确保 WSL2 深度学习可以使用我的 GPU
- javascript - 为什么其他 javaScript 函数无法读取用 javaScript 制作的 div?
- reactjs - 在单元测试中安装时,FormattedTime 组件抛出错误
- javascript - document.querySelectorAll() 不适用于额外 div 中的同一类