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

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

问题描述

书上说函数和闭包都是引用类型". 那么,如何判断引用是否相等呢?== 和 === 不起作用.

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

推荐答案

Chris Lattner 在开发者论坛上写道:

Chris Lattner wrote on the developer forums:

这是我们有意不支持的功能.有各种会导致函数指针相等的事情(在swift 类型系统意义,包括几种闭包)失败或改变取决于优化.如果==="被定义在函数,不允许编译器合并相同的方法身体,共享 thunk,并在中执行某些捕获优化关闭.此外,这种平等将是极其在某些泛型上下文中令人惊讶,在那里您可以获得重新抽象将函数的实际签名调整为函数类型需要.

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天全站免登陆