<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Milos,</div><div class=""><br class=""></div><div class="">Thanks for taking a look at it, I appreciate you suggestion.</div><div class=""><br class=""></div><div class="">It’s protocol-oriented, quite different from the idea I was trying to emulate — the one provided by GameplayKit.</div><div class=""><br class=""></div><div class="">Well, I tried it and it works great.</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Now, would you implement those methods differently?</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">mutating</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> add<T: </span><span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">Component</span><span style="font-variant-ligatures: no-common-ligatures" class="">>(component: </span><span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">T</span><span style="font-variant-ligatures: no-common-ligatures" class="">) {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">self</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">components</span><span style="font-variant-ligatures: no-common-ligatures" class="">[</span><span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">T</span><span style="font-variant-ligatures: no-common-ligatures" class="">.name] = component</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">mutating</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> remove<T: </span><span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">Component</span><span style="font-variant-ligatures: no-common-ligatures" class="">>(</span><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">_</span><span style="font-variant-ligatures: no-common-ligatures" class="">: </span><span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">T</span><span style="font-variant-ligatures: no-common-ligatures" class="">.Type) {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">self</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">components</span><span style="font-variant-ligatures: no-common-ligatures" class="">[</span><span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">T</span><span style="font-variant-ligatures: no-common-ligatures" class="">.name] = </span><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">nil</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div></div><div class=""><br class=""></div><div class="">Also, since the key to the components dictionary is the name, adding a component of the same type will replace the exiting one.</div><div class=""><br class=""></div><div class="">How would you change that so it would be possible to add some components more than once, as Jens mentioned?</div><div class=""><br class=""></div><div class="">Best,</div><div class=""><br class=""></div><div class="">—A</div><br class=""><div><blockquote type="cite" class=""><div class="">On Apr 8, 2016, at 7:12 AM, Milos Rankovic <<a href="mailto:milos@milos-and-slavica.net" class="">milos@milos-and-slavica.net</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">A type-uniquing alternative (see my previous message):<div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; color: rgb(203, 203, 203);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">// Swift 2.2</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; min-height: 15px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; color: rgb(203, 203, 203);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">// Entity-Component System (sketch):</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">protocol</span><span style="font-variant-ligatures: no-common-ligatures" class=""> Component {</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; color: rgb(203, 203, 203);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">static</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">var</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> name: </span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">String</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> { </span><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">get</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> } </span><span style="font-variant-ligatures: no-common-ligatures" class="">// not necessary (see comments below)</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; min-height: 15px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; color: rgb(200, 172, 229);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">Component</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> {</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; color: rgb(203, 203, 203);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span><span style="font-variant-ligatures: no-common-ligatures" class="">// can be overridden with `let` by conforming types</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; color: rgb(200, 172, 229);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span><span style="font-variant-ligatures: no-common-ligatures" class="">static</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">var</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> name: </span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">String</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> { </span><span style="font-variant-ligatures: no-common-ligatures" class="">return</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">String</span><span style="font-variant-ligatures: no-common-ligatures;" class="">(</span><span style="font-variant-ligatures: no-common-ligatures" class="">self</span><span style="font-variant-ligatures: no-common-ligatures;" class="">.</span><span style="font-variant-ligatures: no-common-ligatures" class="">dynamicType</span><span style="font-variant-ligatures: no-common-ligatures;" class="">) }</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; min-height: 15px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">protocol</span><span style="font-variant-ligatures: no-common-ligatures" class=""> Entity {</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">static</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> with(</span><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">_</span><span style="font-variant-ligatures: no-common-ligatures" class="">: </span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">Component</span><span style="font-variant-ligatures: no-common-ligatures" class="">...) -> </span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">Self</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> components: [</span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">String</span><span style="font-variant-ligatures: no-common-ligatures" class="">:</span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">Component</span><span style="font-variant-ligatures: no-common-ligatures" class="">] { </span><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">get</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">set</span><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; color: rgb(200, 172, 229);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span><span style="font-variant-ligatures: no-common-ligatures" class="">init</span><span style="font-variant-ligatures: no-common-ligatures;" class="">()</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> component<T: </span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">Component</span><span style="font-variant-ligatures: no-common-ligatures" class="">>(</span><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">_</span><span style="font-variant-ligatures: no-common-ligatures" class="">: </span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">T</span><span style="font-variant-ligatures: no-common-ligatures" class="">.Type) -> </span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">T</span><span style="font-variant-ligatures: no-common-ligatures" class="">?</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-family: 'Source Code Pro'; font-size: 12px;" class="">}</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; min-height: 15px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; color: rgb(200, 172, 229);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">Entity</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> {</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">static</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> with(components: </span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">Component</span><span style="font-variant-ligatures: no-common-ligatures" class="">...) -> </span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">Self</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">                </span></span><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> d: [</span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">String</span><span style="font-variant-ligatures: no-common-ligatures" class="">:</span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">Component</span><span style="font-variant-ligatures: no-common-ligatures" class="">] = [:]</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">                </span></span><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">for</span><span style="font-variant-ligatures: no-common-ligatures" class=""> c </span><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">in</span><span style="font-variant-ligatures: no-common-ligatures" class=""> components { d[c.</span><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">dynamicType</span><span style="font-variant-ligatures: no-common-ligatures" class="">.name</span><span style="font-variant-ligatures: no-common-ligatures; color: #cbcbcb" class="">/* String(c.dynamicType) */</span><span style="font-variant-ligatures: no-common-ligatures" class="">] = c }</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">                </span></span><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> entity = </span><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">self</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">init</span><span style="font-variant-ligatures: no-common-ligatures" class="">()</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>entity.</span><span style="font-variant-ligatures: no-common-ligatures; color: #8f793a" class="">components</span><span style="font-variant-ligatures: no-common-ligatures" class=""> = d</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">                </span></span><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> entity</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> component<T: </span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">Component</span><span style="font-variant-ligatures: no-common-ligatures" class="">>(</span><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">_</span><span style="font-variant-ligatures: no-common-ligatures" class="">: </span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">T</span><span style="font-variant-ligatures: no-common-ligatures" class="">.Type) -> </span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">T</span><span style="font-variant-ligatures: no-common-ligatures" class="">? {</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; color: rgb(203, 203, 203);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><span class="Apple-tab-span" style="white-space:pre">                </span></span><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">return</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">self</span><span style="font-variant-ligatures: no-common-ligatures;" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #8f793a" class="">components</span><span style="font-variant-ligatures: no-common-ligatures;" class="">[</span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">T</span><span style="font-variant-ligatures: no-common-ligatures;" class="">.name</span><span style="font-variant-ligatures: no-common-ligatures" class="">/* String(T) */</span><span style="font-variant-ligatures: no-common-ligatures;" class="">] </span><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">as</span><span style="font-variant-ligatures: no-common-ligatures;" class="">? </span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">T</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-family: 'Source Code Pro'; font-size: 12px; font-variant-ligatures: no-common-ligatures;" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span></span><span style="color: rgb(203, 203, 203); font-family: 'Source Code Pro'; font-size: 12px; font-variant-ligatures: no-common-ligatures;" class="">// TODO: </span><font color="#cbcbcb" face="Source Code Pro" class=""><span style="font-size: 12px;" class="">mutating </span></font><span style="font-size: 12px; color: rgb(203, 203, 203); font-family: 'Source Code Pro';" class="">func add<T: Component>(_: T)</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-family: 'Source Code Pro'; font-size: 12px; font-variant-ligatures: no-common-ligatures;" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span></span><span style="color: rgb(203, 203, 203); font-family: 'Source Code Pro'; font-size: 12px; font-variant-ligatures: no-common-ligatures;" class="">// TODO: </span><font color="#cbcbcb" face="Source Code Pro" class=""><span style="font-size: 12px;" class="">mutating </span></font><span style="font-size: 12px; color: rgb(203, 203, 203); font-family: 'Source Code Pro';" class="">func remove<T: Component>(_: T.Type)</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; min-height: 15px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; color: rgb(203, 203, 203);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">// game:</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">struct</span><span style="font-variant-ligatures: no-common-ligatures" class=""> Character: </span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">Entity</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> components: [</span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">String</span><span style="font-variant-ligatures: no-common-ligatures" class="">:</span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">Component</span><span style="font-variant-ligatures: no-common-ligatures" class="">] = [:]</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; min-height: 15px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">struct</span><span style="font-variant-ligatures: no-common-ligatures" class=""> Health: </span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">Component</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> percent = </span><span style="font-variant-ligatures: no-common-ligatures; color: #ff2600" class="">100.0</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> dead: </span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">Bool</span><span style="font-variant-ligatures: no-common-ligatures" class=""> { </span><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #8f793a" class="">percent</span><span style="font-variant-ligatures: no-common-ligatures" class=""> <= </span><span style="font-variant-ligatures: no-common-ligatures; color: #ff2600" class="">0</span><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; min-height: 15px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">struct</span><span style="font-variant-ligatures: no-common-ligatures" class=""> Attack: </span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">Component</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> range = </span><span style="font-variant-ligatures: no-common-ligatures; color: #ff2600" class="">0</span><span style="font-variant-ligatures: no-common-ligatures" class="">, damage = </span><span style="font-variant-ligatures: no-common-ligatures; color: #ff2600" class="">0</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; min-height: 15px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; color: rgb(203, 203, 203);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">// use:</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> health = </span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">Health</span><span style="font-variant-ligatures: no-common-ligatures" class="">()</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> attack = </span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">Attack</span><span style="font-variant-ligatures: no-common-ligatures" class="">()</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; min-height: 15px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> character = </span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">Character</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #91540f" class="">with</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #8f793a" class="">health</span><span style="font-variant-ligatures: no-common-ligatures" class="">, </span><span style="font-variant-ligatures: no-common-ligatures; color: #8f793a" class="">attack</span><span style="font-variant-ligatures: no-common-ligatures" class="">)</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; min-height: 15px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; color: rgb(143, 121, 58);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">character</span><span style="font-variant-ligatures: no-common-ligatures;" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #91540f" class="">component</span><span style="font-variant-ligatures: no-common-ligatures;" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">Health</span><span style="font-variant-ligatures: no-common-ligatures;" class="">)?.</span><span style="font-variant-ligatures: no-common-ligatures" class="">percent</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #cbcbcb" class="">// 100</span></div></div><div class=""><br class=""></div><div class=""><div class=""><div class=""><div class="">milos</div><div class=""><br class=""></div><div class=""><blockquote type="cite" class=""><div class="">On 8 Apr 2016, at 11:05, Milos Rankovic via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">This is just a sketch. There may be issues down the line (I’ve indicated some with `TODO`s), but it works and you can try it in the playground:<div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; color: rgb(203, 203, 203);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">// Swift 2.2</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; color: rgb(203, 203, 203);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; color: rgb(203, 203, 203);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">// utility:</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; color: rgb(200, 172, 229);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">Array</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> {</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> first <T> (</span><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">_</span><span style="font-variant-ligatures: no-common-ligatures" class="">: </span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">T</span><span style="font-variant-ligatures: no-common-ligatures" class="">.Type) -> </span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">T</span><span style="font-variant-ligatures: no-common-ligatures" class="">? {</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; color: rgb(200, 172, 229);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><span class="Apple-tab-span" style="white-space:pre">                </span></span><span style="font-variant-ligatures: no-common-ligatures" class="">for</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> e </span><span style="font-variant-ligatures: no-common-ligatures" class="">in</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">self</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">where</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> e </span><span style="font-variant-ligatures: no-common-ligatures" class="">is</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">T</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> { </span><span style="font-variant-ligatures: no-common-ligatures" class="">return</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> e </span><span style="font-variant-ligatures: no-common-ligatures" class="">as</span><span style="font-variant-ligatures: no-common-ligatures;" class="">? </span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">T</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> }</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; color: rgb(200, 172, 229);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><span class="Apple-tab-span" style="white-space:pre">                </span></span><span style="font-variant-ligatures: no-common-ligatures" class="">return</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">nil</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; min-height: 15px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; color: rgb(203, 203, 203);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">// Entity-Component System (sketch):</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">protocol</span><span style="font-variant-ligatures: no-common-ligatures" class=""> Component { }</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; min-height: 15px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">protocol</span><span style="font-variant-ligatures: no-common-ligatures" class=""> Entity {</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">static</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> with(</span><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">_</span><span style="font-variant-ligatures: no-common-ligatures" class="">: </span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">Component</span><span style="font-variant-ligatures: no-common-ligatures" class="">...) -> </span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">Self</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span><span style="color: rgb(203, 203, 203);" class="">// TODO: make get only</span></span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class=""><span style="color: rgb(203, 203, 203);" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>// also </span></span><span style="color: rgb(203, 203, 203);" class="">should be a set-by-type really, but that would </span></div><div style="margin: 0px; line-height: normal;" class=""><span class="Apple-tab-span" style="color: rgb(203, 203, 203); font-family: 'Source Code Pro'; font-size: 12px; white-space: pre;">        </span><font color="#cbcbcb" face="Source Code Pro" class=""><span style="font-size: 12px;" class="">// force `Component` to be a class (which may be worth it)</span></font></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> components: [</span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">Component</span><span style="font-variant-ligatures: no-common-ligatures" class="">] { </span><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">get</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">set</span><span style="font-variant-ligatures: no-common-ligatures" class=""> } </span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; color: rgb(200, 172, 229);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span><span style="font-variant-ligatures: no-common-ligatures" class="">init</span><span style="font-variant-ligatures: no-common-ligatures;" class="">()</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> component<T: </span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">Component</span><span style="font-variant-ligatures: no-common-ligatures" class="">>(</span><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">_</span><span style="font-variant-ligatures: no-common-ligatures" class="">: </span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">T</span><span style="font-variant-ligatures: no-common-ligatures" class="">.Type) -> </span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">T</span><span style="font-variant-ligatures: no-common-ligatures" class="">?</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; min-height: 15px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; color: rgb(200, 172, 229);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">Entity</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> {</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; color: rgb(200, 172, 229);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span><span style="font-variant-ligatures: no-common-ligatures;" class="">static</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures;" class="">func</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> with(components: </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(73, 162, 210);" class="">Component</span><span style="font-variant-ligatures: no-common-ligatures;" class="">...) -> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(73, 162, 210);" class="">Self</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> {</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; color: rgb(200, 172, 229);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><span class="Apple-tab-span" style="white-space:pre">                </span></span><span style="font-variant-ligatures: no-common-ligatures;" class="">var</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> entity = </span><span style="font-variant-ligatures: no-common-ligatures;" class="">self</span><span style="font-variant-ligatures: no-common-ligatures;" class="">.</span><span style="font-variant-ligatures: no-common-ligatures;" class="">init</span><span style="font-variant-ligatures: no-common-ligatures;" class="">()</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; color: rgb(200, 172, 229);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><span class="Apple-tab-span" style="white-space: pre;">                </span><span style="color: rgb(203, 203, 203);" class="">// TODO: enforce uniquely typed elements</span></span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>entity.</span><span style="font-variant-ligatures: no-common-ligatures; color: #8f793a" class="">components</span><span style="font-variant-ligatures: no-common-ligatures" class=""> = components</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">                </span></span><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> entity</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> component<T: </span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">Component</span><span style="font-variant-ligatures: no-common-ligatures" class="">>(</span><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">_</span><span style="font-variant-ligatures: no-common-ligatures" class="">: </span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">T</span><span style="font-variant-ligatures: no-common-ligatures" class="">.Type) -> </span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">T</span><span style="font-variant-ligatures: no-common-ligatures" class="">? {</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; color: rgb(200, 172, 229);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><span class="Apple-tab-span" style="white-space:pre">                </span></span><span style="font-variant-ligatures: no-common-ligatures" class="">return</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">self</span><span style="font-variant-ligatures: no-common-ligatures;" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #8f793a" class="">components</span><span style="font-variant-ligatures: no-common-ligatures;" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #91540f" class="">first</span><span style="font-variant-ligatures: no-common-ligatures;" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">T</span><span style="font-variant-ligatures: no-common-ligatures;" class="">)</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; min-height: 15px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; color: rgb(203, 203, 203);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">// game:</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; color: rgb(203, 203, 203);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">struct</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> Character: </span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">Entity</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> {</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; color: rgb(203, 203, 203);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span></span>// TODO: make private</div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; color: rgb(203, 203, 203);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">var</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> components: [</span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">Component</span><span style="font-variant-ligatures: no-common-ligatures;" class="">] = [] </span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; min-height: 15px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">struct</span><span style="font-variant-ligatures: no-common-ligatures" class=""> Health: </span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">Component</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> percent = </span><span style="font-variant-ligatures: no-common-ligatures; color: #ff2600" class="">100.0</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> dead = </span><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">false</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; min-height: 15px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">struct</span><span style="font-variant-ligatures: no-common-ligatures" class=""> Attack: </span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">Component</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> range = </span><span style="font-variant-ligatures: no-common-ligatures; color: #ff2600" class="">0</span><span style="font-variant-ligatures: no-common-ligatures" class="">, damage = </span><span style="font-variant-ligatures: no-common-ligatures; color: #ff2600" class="">0</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; min-height: 15px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; color: rgb(203, 203, 203);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">// use:</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> health = </span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">Health</span><span style="font-variant-ligatures: no-common-ligatures" class="">()</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> attack = </span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">Attack</span><span style="font-variant-ligatures: no-common-ligatures" class="">()</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; min-height: 15px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> character = </span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">Character</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #91540f" class="">with</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #8f793a" class="">health</span><span style="font-variant-ligatures: no-common-ligatures" class="">, </span><span style="font-variant-ligatures: no-common-ligatures; color: #8f793a" class="">attack</span><span style="font-variant-ligatures: no-common-ligatures" class="">)</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; min-height: 15px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; color: rgb(143, 121, 58);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">character</span><span style="font-variant-ligatures: no-common-ligatures;" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #91540f" class="">component</span><span style="font-variant-ligatures: no-common-ligatures;" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">Health</span><span style="font-variant-ligatures: no-common-ligatures;" class="">)?.</span><span style="font-variant-ligatures: no-common-ligatures" class="">percent</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #cbcbcb" class="">// 100</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; min-height: 15px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div></div><div class="">Hope this helps,</div><div class=""><br class=""></div><div class="">milos</div><div class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On 8 Apr 2016, at 00:47, Adriano Ferreira via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Hi everyone!<div class=""><br class=""></div><div class="">I’m experimenting with <a href="https://en.wikipedia.org/wiki/Entity_component_system" class="">Entity-Component Systems</a> and I’d appreciate if you could help me working on how to check/get custom objects from a collection.</div><div class=""><br class=""></div><div class="">The idea is to verify if an entity contains a particular component and, if so, retrieve it.</div><div class=""><br class=""></div><div class="">Here’s the API I’d like work on:</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(192, 192, 192);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">// Entity Library</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(222, 52, 140);" class="">class</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> Character: </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(88, 126, 168);" class="">Entity</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> {}</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""></span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(192, 192, 192);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">// Component Library</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(222, 52, 140);" class="">class</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> HealthComponent: </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(88, 126, 168);" class="">Component</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(222, 52, 140);" class="">var</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> health </span>= 100.0</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(222, 52, 140);" class="">var</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> isDead </span><span style="font-variant-ligatures: no-common-ligatures;" class="">= </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(222, 52, 140);" class="">false</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">}</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""></span><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(222, 52, 140);" class="">class</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> AttackComponent: </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(88, 126, 168);" class="">Component</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(222, 52, 140);" class="">var</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> range </span>= 0</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(222, 52, 140);" class="">var</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> damage </span>= 0</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">}</span></div></div><div class=""><br class=""></div><div class=""><span style="color: rgb(192, 192, 192); font-family: Menlo; font-size: 11px;" class="">// Usage</span></div><div class=""><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(222, 52, 140);" class="">var</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> healthComponent = </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(88, 126, 168);" class="">HealthComponent</span><span style="font-variant-ligatures: no-common-ligatures;" class="">()</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(222, 52, 140);" class="">var</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> attackComponent = </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(88, 126, 168);" class="">AttackComponent</span><span style="font-variant-ligatures: no-common-ligatures;" class="">()</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""></span><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(88, 126, 168);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(222, 52, 140);" class="">var</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> components: [</span><span style="font-variant-ligatures: no-common-ligatures;" class="">ComponentType</span><span style="font-variant-ligatures: no-common-ligatures;" class="">] = [</span><span style="font-variant-ligatures: no-common-ligatures;" class="">healthComponent</span><span style="font-variant-ligatures: no-common-ligatures;" class="">, </span><span style="font-variant-ligatures: no-common-ligatures;" class="">attackComponent</span><span style="font-variant-ligatures: no-common-ligatures;" class="">]</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(222, 52, 140);" class="">var</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> char = </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(88, 126, 168);" class="">Character</span><span style="font-variant-ligatures: no-common-ligatures;" class="">(components: </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(88, 126, 168);" class="">components</span><span style="font-variant-ligatures: no-common-ligatures;" class="">)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""></span><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(222, 52, 140);" class="">let</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> hc = </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(88, 126, 168);" class="">char</span><span style="font-variant-ligatures: no-common-ligatures;" class="">.get(component: </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(88, 126, 168);" class="">HealthComponent</span><span style="font-variant-ligatures: no-common-ligatures;" class="">)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(222, 52, 140);" class="">let</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> ac = </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(88, 126, 168);" class="">char</span><span style="font-variant-ligatures: no-common-ligatures;" class="">.get(component: </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(88, 126, 168);" class="">AttackComponent</span><span style="font-variant-ligatures: no-common-ligatures;" class="">)</span></div></div></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">So, what are your thoughts on the TODOs below?</div><div class=""><br class=""></div><div class="">—</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">import</span><span style="font-variant-ligatures: no-common-ligatures" class=""> Foundation</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">protocol</span><span style="font-variant-ligatures: no-common-ligatures" class=""> ComponentType {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> entity: </span><span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">EntityType</span><span style="font-variant-ligatures: no-common-ligatures" class="">? { </span><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">get</span><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">protocol</span><span style="font-variant-ligatures: no-common-ligatures" class=""> EntityType {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> components: [</span><span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">ComponentType</span><span style="font-variant-ligatures: no-common-ligatures" class="">] { </span><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">get</span><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> get<T: </span><span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">ComponentType</span><span style="font-variant-ligatures: no-common-ligatures" class="">>(component c: </span><span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">T</span><span style="font-variant-ligatures: no-common-ligatures" class="">.Type) -> </span><span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">T</span><span style="font-variant-ligatures: no-common-ligatures" class="">?</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> add(component c: </span><span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">ComponentType</span><span style="font-variant-ligatures: no-common-ligatures" class="">)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> remove(component c: </span><span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">ComponentType</span><span style="font-variant-ligatures: no-common-ligatures" class="">)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">class</span><span style="font-variant-ligatures: no-common-ligatures" class=""> Component: </span><span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">ComponentType</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> entity: </span><span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">EntityType</span><span style="font-variant-ligatures: no-common-ligatures" class="">?</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">class</span><span style="font-variant-ligatures: no-common-ligatures" class=""> Entity: </span><span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">EntityType</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> components = [</span><span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">ComponentType</span><span style="font-variant-ligatures: no-common-ligatures" class="">]()</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">init</span><span style="font-variant-ligatures: no-common-ligatures" class="">(components: [</span><span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">ComponentType</span><span style="font-variant-ligatures: no-common-ligatures" class="">]) {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">for</span><span style="font-variant-ligatures: no-common-ligatures" class=""> component </span><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">in</span><span style="font-variant-ligatures: no-common-ligatures" class=""> components {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">self</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">add</span><span style="font-variant-ligatures: no-common-ligatures" class="">(component: component)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> get<T: </span><span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">ComponentType</span><span style="font-variant-ligatures: no-common-ligatures" class="">>(component c: </span><span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">T</span><span style="font-variant-ligatures: no-common-ligatures" class="">.Type) -> </span><span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">T</span><span style="font-variant-ligatures: no-common-ligatures" class="">? {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #c0c0c0" class="">// TODO: - not sure how to work the types here</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="color: rgb(192, 192, 192); font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;" class="">// if `self` contains component of given type, return it</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(192, 192, 192);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">// otherwise, return nil</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> add(component c: </span><span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">ComponentType</span><span style="font-variant-ligatures: no-common-ligatures" class="">) {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #c0c0c0" class="">// TODO: - depends on the `get` function</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(192, 192, 192);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">// if `self` already contains component, just return</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(192, 192, 192);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">// otherwise, self.components += [component]</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> remove(component c: </span><span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">ComponentType</span><span style="font-variant-ligatures: no-common-ligatures" class="">) {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #c0c0c0" class="">// TODO: </span><span style="color: rgb(192, 192, 192);" class="">- also depends on the `get` function</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(192, 192, 192);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">// if `self` contains component, remove it</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(192, 192, 192);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">// otherwise, just return</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Best,</div><div class=""><br class=""></div><div class="">—A</div></div>_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-users" class="">https://lists.swift.org/mailman/listinfo/swift-users</a><br class=""></div></blockquote></div><br class=""></div></div>_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-users" class="">https://lists.swift.org/mailman/listinfo/swift-users</a><br class=""></div></blockquote></div><br class=""></div></div></div></div></div></blockquote></div><br class=""></body></html>