如何测试函数和闭包的平等? [英] How do you test functions and closures for equality?

查看:112
本文介绍了如何测试函数和闭包的平等?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这本书说函数和闭包是引用类型。那么,你如何找出引用是否相等呢? ==和===不起作用。

  func a(){} 
letå= a
let b =å===å//无法找到接受所提供参数的===的重载

这里是Catterwauls如何处理这个:



多机械&平等闭包



测试

解决方案

Chris Lattner在开发人员论坛上写道:


这是我们故意不支持的功能。有
各种各样的事情,将导致函数的指针相等(在
swift类型系统感觉,其中包括几种闭包)
失败或根据优化改变。如果在
函数中定义了===,编译器将不允许合并相同的方法
body,共享thunk,并在
闭包中执行某些捕获优化。此外,这种类型的平等在一些泛型上下文中将是非常惊人的,在那里你可以得到重新获取
thunks,将函数的实际签名调整为
函数类型期望的。 / p>

https:// devforums。 apple / message/1035180#1035180



这意味着你不应该尝试比较闭包的相等性,因为优化可能会影响结果。 p>

The book says that "functions and closures are reference types". So, how do you find out if the references are equal? == and === don't work.

func a() { }
let å = a
let b = å === å // Could not find an overload for === that accepts the supplied arguments

Here is how the Catterwauls are dealing with this:

MultiClosures & Equatable Closures

tests

解决方案

Chris Lattner wrote on the developer forums:

This is a feature we intentionally do not want to support. There are a variety of things that will cause pointer equality of functions (in the swift type system sense, which includes several kinds of closures) to fail or change depending on optimization. If "===" were defined on functions, the compiler would not be allowed to merge identical method bodies, share thunks, and perform certain capture optimizations in closures. Further, equality of this sort would be extremely surprising in some generics contexts, where you can get reabstraction thunks that adjust the actual signature of a function to the one the function type expects.

https://devforums.apple.com/message/1035180#1035180

This means that you should not even try to compare closures for equality because optimizations may affect the outcome.

这篇关于如何测试函数和闭包的平等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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