Forked from c9iim/protocol-extension-struct-example.swift
Created
April 10, 2016 18:09
-
-
Save uchcode/223f0eb7d5fa95d48f7a3b45a7689e33 to your computer and use it in GitHub Desktop.
ユーザー定義に依存したプロトコル拡張例。依存部分の初期設定は拡張外の責務。
This file contains hidden or 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
//: ユーザー定義に依存したプロトコル拡張例。依存部分の初期設定は拡張外の責務。 | |
protocol FooSpec { | |
var fizz : Int { get } | |
func buzz() -> Int | |
} | |
extension FooSpec { | |
func buzz() -> Int { | |
return fizz * 2 | |
} | |
} | |
// | |
struct Foo : FooSpec { | |
let fizz : Int | |
init(fizz: Int) { | |
self.fizz = fizz | |
} | |
} | |
let const = 1 | |
let bar = Foo(fizz: const) | |
assert(bar.buzz() == const * 2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment