![](https://s.togetter.com/static/web/img/placeholder.gif)
『プロトコル拡張を、それに準拠する将来の型で制約して、既定の実装を添えよう』という話を発端にした考察
-
es_kumagai
- 1922
- 7
- 2
- 0
![](https://s.togetter.com/static/web/img/placeholder.gif)
とりあえず、型で検証するとこんな感じ。文字列化すると情報が落ちるみたいで混乱しますけど、メタタイプかどうかまでを確認すると違いがはっきり見えてくるみたい。 gist.github.com/d54c8e696772bb… #swift pic.twitter.com/oBEM8MwZwB
2016-03-08 14:23:15![](https://pbs.twimg.com/media/CdAIN5zUUAEaa0I.jpg:medium)
![](https://s.togetter.com/static/web/img/placeholder.gif)
つまり、こんな感じで Self と、静的メソッドにおける self.dynamicType とが一致するはず。… そろそろプロトコルで検証してみよう。 gist.github.com/077615ced690bd… #swift pic.twitter.com/3fexRx2OfY
2016-03-08 14:31:10![](https://pbs.twimg.com/media/CdAKB0MUkAEp_YA.jpg:medium)
![](https://s.togetter.com/static/web/img/placeholder.gif)
まだ、既定の実装を隠蔽することについての検証はしてないですけど、とりあえず先ほどのコードをプロトコル版にしても(やや違えど)同じく動作しているみたい。 gist.github.com/a8f482b2624a7f… #swift pic.twitter.com/3B9AzMKxQm
2016-03-08 14:51:17![](https://pbs.twimg.com/media/CdAOodhUAAAwuyW.jpg:medium)
![](https://s.togetter.com/static/web/img/placeholder.gif)
あとはこんな性質か。プロトコル拡張内では Self.Type でも、そこにプロトコルに準拠した AdoptClass.Type が入っているかもしれない。 gist.github.com/8c5a095b817cdd… #swift pic.twitter.com/i0PKYvetcF
2016-03-08 15:02:03![](https://pbs.twimg.com/media/CdARGFbVAAErMO4.jpg:medium)
![](https://s.togetter.com/static/web/img/placeholder.gif)
先ほどの例に書いたように .Type.Type についても同様なのと、.Type.Type を取りたいとき、SelfCheck.self の次は .dynamicType … か。 #swift
2016-03-08 15:05:17![](https://s.togetter.com/static/web/img/placeholder.gif)
あれ、いちおう型のメタ情報とて dynamicType で取れているのか。 gist.github.com/1ca3a9dc4e0a9a… #swift pic.twitter.com/hKKhpgCwFQ
2016-03-08 15:06:04![](https://pbs.twimg.com/media/CdASBGfUsAAX2WR.jpg:medium)
![](https://s.togetter.com/static/web/img/placeholder.gif)
こんな感じで。 gist.github.com/7ab9425c058c24… #swift pic.twitter.com/Blk7GQkYao
2016-03-08 15:08:33![](https://pbs.twimg.com/media/CdASla6UkAAC8EH.jpg:medium)
![](https://s.togetter.com/static/web/img/placeholder.gif)
掌握した…? 頭が混乱していますけど、たぶんこれで Self と self.dynamicType で同じ動作になるのが説明できている、はず? gist.github.com/7d6de5261ff03f… #swift pic.twitter.com/9Wh6IBvsIW
2016-03-08 15:57:16![](https://pbs.twimg.com/media/CdAdvHeUsAA_SyN.jpg:medium)
![](https://s.togetter.com/static/web/img/placeholder.gif)
ああ、やっぱりこれで良いのかもしれない。静的メソッドとインスタンスメソッドとで違いが出るはず!と思ったけれど出ず、でも勘違いできっとこれで正しそう。 gist.github.com/78852404b91850… #swift pic.twitter.com/Ebis0pM3m8
2016-03-08 16:15:15![](https://pbs.twimg.com/media/CdAh2XEVIAA8gm6.jpg:medium)
![](https://s.togetter.com/static/web/img/placeholder.gif)
あれ、実行結果が変わらなかった …今件の元の問題は、付属型があるかどうかで動作が変わったはず…! gist.github.com/3cb1ea2abd198f… #swift pic.twitter.com/31hZNPCtNG
2016-03-08 16:22:00![](https://pbs.twimg.com/media/CdAjZVMUUAAaMnE.jpg:medium)
![](https://s.togetter.com/static/web/img/placeholder.gif)
ああ、なるほど。最初の例は extension MyProtocol where Self : ClassA みたいな制約がつくのね。 #swift
2016-03-08 16:26:43![](https://s.togetter.com/static/web/img/placeholder.gif)
そして今、完璧に掌握した😎 … ように思える。また新たな課題を見つけてくれそうですけれどね😉 gist.github.com/bbb87b8135c298… #swift pic.twitter.com/fy2o4anwam
2016-03-08 16:49:59![](https://pbs.twimg.com/media/CdApzI1UsAAxXG6.jpg:medium)
![](https://s.togetter.com/static/web/img/placeholder.gif)
そしてそんな調査の過程で「Swift 史上、最も自己主張の強いコード」を発見した 😏 gist.github.com/152aa27934a904… #swift pic.twitter.com/hrRZuSIhjr
2016-03-08 16:57:52![](https://pbs.twimg.com/media/CdArmuhUAAQDsXE.jpg:medium)
![](https://s.togetter.com/static/web/img/placeholder.gif)
ああ、そうか、もうひとつ。いくら Type の dynamicType と言っても、実行時にしか得られない以上、それを扱うための変数の型はビルド時に何かに決めておかないといけない。だから Self.Type で固定されるのかな。 #swift
2016-03-08 18:24:07![](https://s.togetter.com/static/web/img/placeholder.gif)
そしてこの Self.Type と型へ義務付けるか否かの2つで、もしかしてプロトコル拡張の呼び出し的な動きのほとんどを説明できてしまうかもしれない? #swift
2016-03-08 18:25:48![](https://s.togetter.com/static/web/img/placeholder.gif)
@es_kumagai なるほど。ありがとうございます! 混乱の元凶の print は型情報を深くまで見てくれるということでしょうか…。挙動としては納得できて安心なような、やっぱりよくわからないような。もう少し考えてみます!
2016-03-08 20:06:49![](https://s.togetter.com/static/web/img/placeholder.gif)
@mo_to_44 制限をつけたとき「この場合は"例外的"にこうするよ」とも見えて、果たして例外だけどデフォルトとは一体なんだろうと混乱しまして…。
2016-03-08 20:21:10![](https://s.togetter.com/static/web/img/placeholder.gif)
@mo_to_44 あ!これは、プロトコル側から考えてるからかもしれないです。型側から考えると疑いようもなくデフォルト実装な気もします!
2016-03-08 20:21:44![](https://s.togetter.com/static/web/img/placeholder.gif)
少し混乱したけれど、トリッキーな事態を除いて考えれば、デフォルト実装だと自信を持って言えそうです。 ありがとうございます!\\\\└('ω')」////
2016-03-08 20:43:41![](https://s.togetter.com/static/web/img/placeholder.gif)
最近、トリッキーなことを考えすぎて見失っていたけれど、rizumita さんのおっしゃっていた通り、protocol extension はやはりそのプロトコルの特性として捉えて良いんだ。
2016-03-08 20:59:02![](https://s.togetter.com/static/web/img/placeholder.gif)
たまにはちょっと C++ で。Swift プロトコル拡張の動きを見てたら C++ のクラスの動き「隠蔽」と「オーバーライド」の差異を思い出した。 gist.github.com/908f93a41c5135… #swift #cpp pic.twitter.com/nvGPygUucN
2016-03-08 21:39:28![](https://pbs.twimg.com/media/CdBsDrsUEAESVc7.jpg:medium)
![](https://s.togetter.com/static/web/img/placeholder.gif)
プロトコル拡張の処理的な観点から。C++ でクラスを型で扱ったときと、Swift でプロトコル側から見たときの動きと。C++ でクラスをポインターで扱ったときと、Swift で型側から見たときの動きと。なんかとっても似ている気がした。 #swift
2016-03-08 21:41:59