<div dir="ltr">Hi Nevin (et al) <div><br></div><div>Here's the relevant section of my code. It's from a protocol I use called "ParameterSet" to extend OptionSets to contain small numbers (like an Enum with an associated type, but all the data is stored in the UInt). Wouldn't be my first choice to share, as it breaks KISS. Anyhow, the "ExpressibleByDictionaryLiteral" lets me specify a "params" template tersely. I only found out how to use the -Literal initializers last year. I love them.</div><div><div><br></div><div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                </span>struct DrumNote: ParameterSet {</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                        </span></div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                        </span>let rawValue: UInt</div><div><br></div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                        </span>static let params: [ParamType] = [ ["velocity":0...255], "flam", "roll", "reverse" ]</div><div><br></div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                        </span>static let</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                                </span>velocity<span class="gmail-Apple-tab-span" style="white-space:pre">        </span>= param(0),</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                                </span>flam<span class="gmail-Apple-tab-span" style="white-space:pre">                </span>= param(1),</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                                </span>roll<span class="gmail-Apple-tab-span" style="white-space:pre">                </span>= param(2),</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                                </span>reverse<span class="gmail-Apple-tab-span" style="white-space:pre">        </span>= param(3),</div><div><br></div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                        </span>// ...</div><div><br></div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                </span>}</div><div><br></div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                </span>enum ParamType: ExpressibleByStringLiteral, ExpressibleByDictionaryLiteral {</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                        </span></div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                        </span>public init( dictionaryLiteral elements: (String,Any)... ) {</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                                </span>if let pair = elements.first as? (String,ClosedRange<FloatLiteralType>) { self = .doub(pair.0,pair.1) }</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                                </span>else if let pair = elements.first as? (String,CountableClosedRange<IntegerLiteralType>) { self = .int(pair.0, pair.1.lowerBound ... pair.1.upperBound ) }</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                                </span>else { fatalError("Can only init from Double...Double or Int...Int") }</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                        </span>}</div><div><br></div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                        </span>// ...</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                        </span></div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                </span>}</div></div><div><br></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Jan 9, 2018 at 6:10 PM, Nevin Brackett-Rozinsky <span dir="ltr"><<a href="mailto:nevin.brackettrozinsky@gmail.com" target="_blank">nevin.brackettrozinsky@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Tue, Jan 9, 2018 at 7:47 PM, <a href="mailto:charles@charlesism.com" target="_blank">charles@charlesism.com</a> <span dir="ltr"><<a href="mailto:charlesism.com@gmail.com" target="_blank">charlesism.com@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="auto"><div>I used a DictionaryLiteral only yesterday, and it turned what would have a typically unreadable array of Structs into something much more elegant. I'm pretty sure the only reason Literals (of all varieties) aren't used more often is because Swift programmers don't realize they are available and easy to implement. <br></div></div></blockquote><div><br></div><div>Could you provide an example of how you used DictionaryLiteral?</div><span class="gmail-HOEnZb"><font color="#888888"><div><br></div><div>Nevin </div></font></span></div></div></div>
</blockquote></div><br></div></div></div>