Does Swift support aspect oriented programming?


The foundation ofAspect Oriented Programmingis the intercept pattern. We start with a crosscutting requirement - something that needs to occur in many parts of the application. And then using apointcut expression, modularize it, by identifying all of the places this requirement should be applied. This is done by intercepting a method call and weaving in additional behavior. Therefore, for a language to support AOP, it must support the intercept pattern.

Now, depending on the language, method interception can be applied either at compile-time, run-time or both. Swift is an interesting case in this regard, as it supports the following kinds of method dispatch:

  • Static/vtable, like C++(faster : in tests accounted for about 1.1 nano-seconds or less of method invocation time)
  • Messaging, like Objective-C(slower : in tests accounted for about 4.9 nano-seconds of method invocation time). Also known asdynamic dispatchorlate binding.

If you extend NSObject or use the @objc decoration then messaging will be used. Otherwise Swift will revert to static/vtable method invocations.

  • With the static/vtable type of dispatch, only compile-time interception is possible. In the case of C++ (and Swift) this involves using a preprocessor that generates new source, prior to actual compilation. This is a somewhat cumbersome approach and takes more effort to develop the necessary tools. Although it does give the best possible performance.
  • With the messaging style of method invocation run-time interception is also available. In fact, Objective-C made interception so easy that there was no formal AOP framework. It might've been useful, but "raw materials" were so good no one bothered to make one. Many of Cooca's best features take advantage of Objective-C's dynamic dispatch and the ability to intercept method calls.

Summary:

  • Swift will support runtime AOP if you extend NSObject or use the '@objc' decoration. There are some quirks and limitations to this - Apple's guide on using KVO with Swift will point out most of them.
  • If you don't extend an Objective-C base or use the '@objc' decoration, then only compile-time AOP will be possible. As yet there is no such library to provide compile-time AOP. Furthermore, a disadvantage of compile-time AOP is that it only works with classes that you have the source for.

NB1:Some languages, such as Java uses a static/vtable style of method dispatch and still support runtime method interception. This is possible as they rely on virtual machine, along with aclass loader, another hook-in point. In fact Java is still classed as a 'late binding' language because of this.

NB2:Its technically possible to support provide compile time weaving against compiled-to-machine-code binaries with some limitations. The first is there aren't too many tools to support this because implementation effort is high, and must be repeated per-platform. The second is that it limits the available AOP features.

https://stackoverflow.com/questions/24136535/does-swift-support-aspect-oriented-programming

优质内容筛选与推荐>>
1、数据结构与算法(七)-树
2、3.PopupWindow、拍照、裁剪
3、Facebook正式宣布收购BloomsburyAI
4、反射基础入门篇
5、容器生态系统


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号