We learned in the Protocol-Oriented Programming session at WWDC 2015 that Swift uses two different dispatch mechanisms for methods in protocol extensions.

 

Methods that are protocol requirements — that is, they are declared in the protocol itself — are dispatched dynamically, whereas methods that are not backed by a requirement use static dispatch.

I remember wondering last year why Swift made that distinction. It didn’t make sense to me and, like others, I was concerned it had the potential for lots of hard-to-find bugs. (Turns out it hasn’t been a problem in practice for me so far.) I recently came across this post by Kevin Ballard on Swift Evolution that includes the best explanation I have seen for why method dispatch in protocols works the way it does:

[Protocol extensions] are strictly static typing, all the way. Which makes sense, because there’s no virtual function table (or in Swift terms, protocol witness table) to put the methods into. Extensions can provide default implementations of protocol methods because the type that conforms to the protocol puts the extension method implementation into its own protocol witness table (and they only do this if the type doesn’t already implement the method itself). Since the protocol witness table only contains things defined in the protocol itself, protocol extension methods that aren’t part of the protocol don’t get put anywhere. So invoking one of those methods has no possible virtual function table to check, the only thing it can do is statically invoke the method from the extension. And this is why you can’t override them (or rather, why your override isn’t called if the method resolution is done via the protocol).

The only way to make protocol extension methods work via virtual dispatch is to define a new protocol witness table for the extension itself, but types that were defined without being able to see the extension won’t know to create and populate this protocol witness table. […]

The only other solution that comes to mind is turning all protocol dispatch into dynamic dispatch, which I hope you’ll agree is not a good idea.

So essentially, while protocols have a virtual function table, protocol extensions do not, and cannot easily have one because a type adopting the protocol won’t necessarily know about all extensions at compile time and therefore cannot add the extension methods to its own vtable. Opinions may vary whether dispatching protocols dynamically all the time would be a viable alternative, but it’s clearly not what the Swift team has in mind for the language.

If you want to learn more about how Swift types are laid out internally in memory and what information they contain, take a look at the Swift ABI document. It’s very interesting.

https://oleb.net/blog/2016/06/kevin-ballard-swift-dispatch/

Method Dispatch in Protocol Extensions的更多相关文章

  1. 多态,动态方法调度(dynamic method dispatch)?

    8.多态Polymorphism,向上转型Upcasting,动态方法调度(dynamic method dispatch) 什么叫多态?简言之,马 克 - t o - w i n:就是父类引用指向子 ...

  2. Which dispatch method would be used in Swift?-Existential Container

    In this example: protocol MyProtocol { func testFuncA() } extension MyProtocol { func testFuncA() { ...

  3. Which dispatch method would be used in Swift?

    In this example: protocol MyProtocol { func testFuncA() } extension MyProtocol { func testFuncA() { ...

  4. protocol

    For every object that can have a delegate, there is a corresponding protocol that declares themessag ...

  5. Java中的virtual method

    今天看jcvm的标准的 时候,看到有一个virtual method,感觉很疑惑,以前看Java的时候并没有发现有这类方法. 百度.Google了一下,才发现,Java中普通方法就是virtual m ...

  6. Method Invocation Expressions

      15.12.1. Compile-Time Step 1: Determine Class or Interface to Search   The first step in processin ...

  7. IOS开发protocol使用

    1.什么是protocol? protocol(协议)是用来定义对象的属性和行为,用于回调. 2.protocol分类? 协议中有三个修饰关键字@required和@optional和@propert ...

  8. Dynamic dispatch

    Dynamic dispatch动态调度.动态分发 In computer science, dynamic dispatch is the process of selecting which im ...

  9. An Introduction to Protocol Oriented Programming in Swift

    swift面向协议编程的根本原因在于值类型的存在:面向对象必须要有引用类型的支持: Protocol Oriented approach was introduced to resolve some ...

随机推荐

  1. SE11 数据表中 日志数据更改 勾选的作用

        [园工]HF-abap-Rainy(574570549)  11:10:12这个有啥作用,勾上了怎么查修改日志呢,[园丁]SH-CRM-ALEX(8738890)  11:13:53SCU3[ ...

  2. sabaki and leelazero

    https://tieba.baidu.com/p/5462772621?see_lz=1 http://zero.sjeng.org/ https://www.jianshu.com/p/a4ba1 ...

  3. Ubuntu linux 返回上一次访问的目录

    cd - (cd空格 减号)返回最近一次访问的目录 这个非常方便.平时经常用终端切换目录,能够方便地回到原来的目录就很爽了. jiqing@jiqing-pad:/usr/local/redis/sr ...

  4. 【转】Google 发布 Android 性能优化典范(比较老,但很实用)

    2015年伊始,Google发布了关于Android性能优化典范的专题, 一共16个短视频,每个3-5分钟,帮助开发者创建更快更优秀的Android App.课程专题不仅仅介绍了Android系统中有 ...

  5. I.MX6 MAC Address 导致的系统崩溃

    /**************************************************************************** * I.MX6 MAC Address 导致 ...

  6. 后缀数组 hash求LCP BZOJ 4310: 跳蚤

    后缀数组的题博客里没放进去过..所以挖了一题写写 充实下博客 顺便留作板子.. 一个字符串S中 内容不同的子串 有 sigma{n-sa[i]+1-h[i]}   (噢 这里的h[]就是大家熟知的he ...

  7. bzoj2743 [HEOI2012]采花——树状数组+离线

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2743 和 HH的项链 那道题很像,也是类似的做法: 但不同的是这里的点只有有前驱时才起作用: ...

  8. 重置HTML

    html,body,ul,li,ol,dl,dd,dt,p,h1,h2,h3,h4,h5,h6,form,fieldset,legend,img{margin:0;padding:0}fieldset ...

  9. CodeForces 730A Toda 2 (模拟)

    题意:给定一个序列,现在你每次至多给5个人的权值减小1,最少2个人,最小是0,使得剩下的所有权值都相等且尽量大. 析:用multiset来模拟,每次取权值最大的三个或者两个,直到最后相等.我开始没有这 ...

  10. docker 端口被占用问题解决

    启动容器A, A的端口映射是 80:8080 外部的25000端口映射到服务内部的8080端口:有时候将容器关闭,重新构建镜像及启动容器时会出现一些报错, 比如端口被占用的报错,但通过docker p ...