- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
Swift 1.1 包含 ~> 运算符的声明:
infix operator ~> {
associativity left
precedence 255
}
这在 Swift 中有什么用?它似乎已声明,但没有定义利用它的函数。其他开发人员已将它用于响应模式和队列之间的编码(marshal)处理闭包,但我想知道为什么它在标准框架中定义。我推测它是为了“保留”一个自定义运算符供开发人员使用,因为它具有尽可能高的优先级。
最佳答案
由于 Swift 已经开源,我们可以看到包含 ~>
的真正原因。在标准库中:as a workaround for method specialization in a child protocol in Swift 1.x .
// Workaround for <rdar://problem/14011860> SubTLF: Default
// implementations in protocols. Library authors should ensure
// that this operator never needs to be seen by end-users. See
// test/Prototypes/GenericDispatch.swift for a fully documented
// example of how this operator is used, and how its use can be hidden
// from users.
infix operator ~> { associativity left precedence 255 }
可以在 test/Prototypes/GenericDispatch.swift 中找到详细示例.
注意:不要使用~>
在 Swift 2+ 中。这是一个历史解决方法。它不再需要。继续阅读。
~>
作品在 Swift 2 中,唯一剩下的 ~>
实例是 abs
功能(也可能会消失)。我们可以看到实际上~>
作品。来自 stdlib/public/core/IntegerArithmetic.swift.gyb , SignedInteger
协议(protocol)定义了一个 ~>
运算符(operator):
struct _Abs {}
protocol SignedNumber: Comparable {
prefix func -(_: Self) -> Self
func ~> (_: Self, _: (_Abs, ()) -> Self
}
func ~> <T: SignedNumber>(x: T, _: (_Abs, ()) -> T {
return x < 0 ? -x : x
}
在这里,箭头的 RHS ~>
指定可以向对象发送什么命令。想到p~>(cmd, args)
就像 p.cmd(args)
.
然后我们提供 _Abs
的默认实现当给出 SignedNumber
.
protocol AbsoluteValuable : SignedNumber {
static func abs(_: Self) -> Self
}
func ~> <T: AbsoluteValuable>(x: T, _: (_Abs, ())) -> T {
return T.abs(x)
}
接下来,我们有一个子协议(protocol),AbsoluteValuable
,这可能比 x < 0 ? -x : x
更有效地计算绝对值.然后我们专门研究 ~>
AbsoluteValuable
的运算符.
func abs<T: SignedNumber>(_ x: T) -> T {
return x ~> (_Abs(), ())
}
最后我们隐藏~>
调用公共(public)包装器方法。如果T
实际上是一个 AbsoluteValuable
,越专业,效率越高~>
将被选中。
这也是我们得到 42 ~> _advance(12)
的原因或 42 ~> _distanceTo(23)
如图@rintaro's answer ,因为 .advanceBy
和 .distanceTo
一般 ForwardIndexType
的方法是 O(n) ,但如果类型为 RandomAccessIndexType
,则可以在 O(1) 中实现.
~>
这种模式也可以在不调用 ~>
的情况下完成。运算符,使用协议(protocol)扩展:
protocol SignedInteger: Comparable {
prefix func -(_: Self) -> Self
func genericAbs() -> Self
}
extension SignedInteger {
func genericAbs() -> Self {
return self < 0 ? -self : self
}
}
protocol AbsoluteValueable: SignedInteger {
static func abs(_: Self) -> Self
}
extension AbsoluteValueable {
func genericAbs() -> Self {
return Self.abs(self)
}
// normally you would allow subtypes to override
// genericAbs() directly, instead of overriding
// static abs().
}
func abs<T: SignedInteger>(x: T) -> T {
return x.genericAbs()
}
特别是这就是为什么所有其他 ~>
除abs
之外的实现在 Swift 2 中消失了:所有使用此技术的特化都已更改为使用更明显的协议(protocol)扩展,例如
underestimateCount
在 commit c48d6aa0 (2015 Apr 16) advancedBy
, distanceTo
在 commit 311baf73 (2015 Aug 4) 注意:雷达问题 14011860 并未公开,但我们可以通过其在 OpenRadar 上的副本看到该错误的范围:
关于swift - Swift 中使用的 ~>(波浪号大于)运算符是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26758297/
我不太确定为什么较大字符串(“cat”和“dog”)的答案不一致。我正在用链接列表和模板的使用做一些事情。我的好奇心促使我修改模板和函数重载。如果有人能解释发生了什么,我将不胜感激。谢谢你。 #inc
目前我必须编写这样的查询 SELECT * FROM table WHERE value1 > 5000 OR value2 > 5000 OR value3 > 5000 OR value4 > 5
我想创建一个如下所示的查询,但我不确定如何正确编码, 我希望它在开始时间的 1 小时内返回所有预订,这是我想出的: SELECT BookingId, StartTime FROM Booking W
这个问题已经有答案了: How to check if a number is between two values? (12 个回答) 已关闭 6 年前。 我目前正在 Codecademy 上学习
我想验证用户输入。如果用户输入的数字大于 3,则应抛出错误“Too high”,如果小于 0.15,则应抛出“Too low”错误。如果它在 3 到 0.15 之间,那么它应该显示“好的”。 我的代码
我有一个拖动脚本,我在其中拖动 div.slider,我正在跟踪 div.slider 的“左”值,并在它大于 68 时让它淡出,但问题是它当它达到 6 而不是 68 时淡出。如果我将数字更改为 85
是否有一种常见的模式如何在数据库(postgresql)中存储这样的条件,然后以简单的方式从数据库中获取这些数据,并在前端将其与我们在前端的值 SE 进行比较(以获得正确的“值” "): condit
如何大于/小于内部工作 如果我将 5 与 100 与 5 与 2,147,483,647 (Integer.MAX_VALUE) 进行比较,性能会受到多大影响 5 < 100 and 5 < Inte
当我运行此查询时它有效 SELECT sum( amount ) AS balance FROM balance WHERE amount >= 100 但是当我想过滤用户 ID 时,它返回 NULL
我有下表: account(id, balance, bank_branch) 我想选择账户余额大于其 bank_branch 平均余额的所有账户 我试过了 Select id from accoun
你们有没有人知道如何搜索所有大于指定数字的数字? 例如:所有单据编号>65 我试过这样:documentNumber: [65 TO *] 但我收到异常,因为 lucene 期望解析一个没有 * 的数
我正在使用 Prolog 算法,并且有一个生成抽象语法树的程序,例如 plus(num(1),num(2))这只是 1+2 .这是通过使用 DCG 来完成的。在这个例子中 plus(num(1),nu
是否使用 Sin(720) 或 Cos(1440)(以度为单位的角度)? 无论是在计算机编程中还是在任何其他情况下? 一般来说,是否有任何角度的 Sin/Cosine/Tan 使用 大于360? 在物
我发现了一些与此相关的问题,但没有一个真正回答了我的问题。 我有一个像这样的表格文件: 2 10610 0 0 0 0.0105292 2 10649 0 0 0
我是 Prolog 的新手,我正在尝试解决这个练习: Define a predicate greater_than/2 that takes two numerals in the notation
我想选择具有出现次数的不同键,此查询似乎有效: SELECT ItemMetaData.KEY, ItemMetaData.VALUE, count(*) FROM ItemMetaData GROU
我需要存储和使用大于 ULLONG_MAX 的数值。 我需要对这些值进行算术运算,所以我认为存储为 char** 不是一个选项。 在这些情况下,有没有办法动态创建额外的 long 前缀? 谢谢大家。根
我是 Prolog 的新手,我正在尝试解决这个练习: Define a predicate greater_than/2 that takes two numerals in the notation
处理已知大小但大于 64 位的位掩码(即执行所有位操作)的最有效的数据结构是什么? 字节[]? 大整数?完全是别的东西吗? 需要与 Java 7 兼容,并且对于诸如此类的事情应该很快(或者至少与合理预
编辑:抱歉进行了许多编辑。我自己都忘记写了什么了。 我使用 JPanel,将 BoxLayout 作为 JFrame 的根面板。我向此根面板添加了另外两个面板:带有 FlowLayou 的 Butto
我是一名优秀的程序员,十分优秀!