// 任意のコレクションを受け取る関数(Elementの型は指定しない) func foo<T: Collection>(collection: T) // 任意のIntのコレクションを受け取る関数 func foo<T: Collection<Int>>(collection: T) var collection: any Collection= [1, 2, 3] print(collection.first!) // firstの型はAny?になってしまう var intCollection: any Collection<Int> = [1, 2, 3] print(intCollection.first!) // firstの型はInt?に定まる このCollection<Int>のような記法は、「主要関連型(Primary Associated Types)に対して同型制約(S
![[Swift] 主要関連型(Primary Associated Types)を持つプロトコルと「ジェネリックなプロトコル」の違い](https://cdn-ak-scissors.b.st-hatena.com/image/square/91f41357ae3e3c9182eca69409a3fd3d9f63efef/height=288;version=1;width=512/https%3A%2F%2Fres.cloudinary.com%2Fzenn%2Fimage%2Fupload%2Fs--lWDmeZbr--%2Fc_fit%252Cg_north_west%252Cl_text%3Anotosansjp-medium.otf_55%3A%25255BSwift%25255D%252520%2525E4%2525B8%2525BB%2525E8%2525A6%252581%2525E9%252596%2525A2%2525E9%252580%2525A3%2525E5%25259E%25258B%252528Primary%252520Associated%252520Types%252529%2525E3%252582%252592%2525E6%25258C%252581%2525E3%252581%2525A4%2525E3%252583%252597%2525E3%252583%2525AD%2525E3%252583%252588%2525E3%252582%2525B3%2525E3%252583%2525AB%2525E3%252581%2525A8%2525E3%252580%25258C%2525E3%252582%2525B8%2525E3%252582%2525A7%2525E3%252583%25258D%2525E3%252583%2525AA%2525E3%252583%252583%2525E3%252582%2525AF%2525E3%252581%2525AA%2525E3%252583%252597...%252Cw_1010%252Cx_90%252Cy_100%2Fg_south_west%252Cl_text%3Anotosansjp-medium.otf_37%3AMiwa%252520%25252F%252520Ensan%252Cx_203%252Cy_121%2Fg_south_west%252Ch_90%252Cl_fetch%3AaHR0cHM6Ly9zdG9yYWdlLmdvb2dsZWFwaXMuY29tL3plbm4tdXNlci11cGxvYWQvYXZhdGFyLzkyNDQ4NzBlMTUuanBlZw%3D%3D%252Cr_max%252Cw_90%252Cx_87%252Cy_95%2Fv1627283836%2Fdefault%2Fog-base-w1200-v2.png)