Last active
September 26, 2017 09:31
-
-
Save omochi/844f68cd6f44bf8d8c3537c2c81e4577 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// あるプロトコルP0が `var aaa` を要求している。 | |
protocol P0 { | |
var aaa: String { get set } | |
} | |
// ここでプロトコルP1は、`var bbb` を与えると `var aaa` を実装してくれる。 | |
protocol P1 { | |
var aaa: String { get set } | |
var bbb: String { get set } | |
} | |
extension P1 { | |
var aaa: String { | |
get { return bbb } | |
set { bbb = newValue } | |
} | |
} | |
// これをS0で使うと以下。 | |
struct S0 : P0, P1 { | |
var bbb: String = "" | |
} | |
// 全く同じことを、 `: class` をつけて行う。 | |
protocol PC0 : class { | |
var aaa: String { get set } | |
} | |
protocol PC1 { | |
var aaa: String { get set } | |
var bbb: String { get set } | |
} | |
extension PC1 { | |
var aaa: String { | |
get { return bbb } | |
set { bbb = newValue } | |
} | |
} | |
// すると以下でコンパイルエラー | |
class C0 : PC0, PC1 { | |
var bbb: String = "" | |
} | |
/* | |
Playground execution failed: | |
error: TempGround.playground:31:7: error: type 'C0' does not conform to protocol 'PC0' | |
class C0 : PC0, PC1 { | |
^ | |
TempGround.playground:27:9: note: candidate is marked 'mutating' but protocol does not allow it | |
set { bbb = newValue } | |
^ | |
TempGround.playground:16:9: note: protocol requires property 'aaa' with type 'String'; do you want to add a stub? | |
var aaa: String { get set } | |
^ | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
protocol PC1
をprotocol PC1 : class
にするか、protocol PC1 : PC0
にするとエラーが無くなる