Last active
February 1, 2017 10:18
-
-
Save Anton3/b82d3dfbd07599410d234c43e94738aa 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
// Feature 1: @mixin extension | |
struct Pizza { | |
var containsCheese: Bool | |
init(cheese: Bool) { ... } | |
func prepare() { print(self) } | |
} | |
struct Margherita { | |
var pizza: Pizza | |
init() { pizza = Pizza(cheese: true) } | |
} | |
@mixin(pizza) extension Margherita { | |
func prepare() | |
} | |
// Usage | |
let margherita = Margherita() | |
margherita.prepare() //=> Pizza(containsCheese: true) | |
mergherita.pizza.prepare() //=> Pizza(containsCheese: true) | |
margherita.containsCheese // error | |
margherita.pizza.containsCheese //=> true | |
margherita as Pizza // error | |
// Feature 2: @mixin structs | |
protocol Orderable { | |
func makeOrder() | |
func meetCourier() | |
} | |
// Mixin structs are not really types; they have requirements to be met in outer type | |
@mixin struct Pizza : Orderable { | |
func prepare() { makeOrder(); meetCourier() } | |
} | |
struct Margherita { | |
var pizza: Pizza | |
func makeOrder() { ... } | |
func meetCourier() { ... } | |
} | |
@mixin(pizza) extension Margherita { | |
func prepare() | |
} | |
// Example: diamond pattern, split base | |
struct A { ... } | |
struct B { var a: A } | |
@mixin(a) extension B { ... } | |
struct C { var a: A } | |
@mixin(a) extension C { ... } | |
struct D { var b: B; var c: C } | |
@mixin(b) extension D { ... } | |
@mixin(c) extension D { ... } | |
// Example: diamond pattern, shared base | |
struct A { ... } | |
protocol HasA { var a: A { get set } } | |
struct B : HasA { ... } | |
struct C : HasA { ... } | |
struct D { var a: A; var b: B; var c: C } | |
@mixin(a) extension D { ... } | |
@mixin(b) extension D { ... } | |
@mixin(c) extension D { ... } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment