<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=""><br class=""><div><blockquote type="cite" class=""><div class="">On Dec 4, 2015, at 4:26 PM, John McCall &lt;<a href="mailto:rjmccall@apple.com" class="">rjmccall@apple.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div style="" class=""><blockquote type="cite" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">On Dec 4, 2015, at 1:19 PM, plx &lt;<a href="mailto:plxswift@icloud.com" class="">plxswift@icloud.com</a>&gt; wrote:<br class=""># A `wrapper` / `deriving` Construct<br class=""><br class="">I'm sure a construct along these lines has been requested numerous times and is hopefully already on the roadmap.<br class=""><br class="">The point of this email is to put out a reasonably-*concrete* sketch as a way of soliciting community feedback on the specifics of how such a construct might look-and-work within Swift; hopefully I’ve gone far-enough to be interesting, but not too much further than that.<br class=""><br class="">## Design Sketch<br class=""><br class="">It ought to be possible to write something like this:<br class=""><br class="">&nbsp;&nbsp;// an example:<br class="">&nbsp;&nbsp;struct SectionIndex<br class="">&nbsp;&nbsp;&nbsp;&nbsp;wrapping Int<br class="">&nbsp;&nbsp;&nbsp;&nbsp;as index<br class="">&nbsp;&nbsp;&nbsp;&nbsp;satisfying precondition { $0 &gt;= 0 }<br class="">&nbsp;&nbsp;&nbsp;&nbsp;deriving Equatable, Comparable, Hashable {<br class="">&nbsp;&nbsp;&nbsp;&nbsp;// declaration can continue in here<br class="">&nbsp;&nbsp;}<br class=""><br class="">...which, when compiled, would be "expanded" along these lines:<br class=""><br class="">&nbsp;&nbsp;struct SectionIndex {<br class=""><br class="">&nbsp;&nbsp;&nbsp;&nbsp;// would have been `wrappedValue` w/out the `as index` clause<br class="">&nbsp;&nbsp;&nbsp;&nbsp;let index: Int<br class=""><br class="">&nbsp;&nbsp;&nbsp;&nbsp;init(_ index: Int) {<br class="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;precondition(index &gt;= 0)<span class="Apple-converted-space">&nbsp;</span><br class="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// ^ would have been assert(index &gt;= 0)<span class="Apple-converted-space">&nbsp;</span><br class="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// &nbsp;&nbsp;had we used `satisfying { $0 &gt;= 0 }`,<br class="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// &nbsp;&nbsp;and omitted entirely had we omitted a `satisfying` clause<br class="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.index = index<br class="">&nbsp;&nbsp;&nbsp;&nbsp;}<br class=""><br class="">&nbsp;&nbsp;}<br class=""><br class="">&nbsp;&nbsp;extension SectionIndex : Equatable {<br class="">&nbsp;&nbsp;}<br class=""><br class="">&nbsp;&nbsp;// synthesized unless explicitly written-out<br class="">&nbsp;&nbsp;func ==(lhs: SectionIndex, rhs: SectionIndex) -&gt; Bool {<br class="">&nbsp;&nbsp;&nbsp;&nbsp;return lhs.index == rhs.index<br class="">&nbsp;&nbsp;}<br class=""><br class="">&nbsp;&nbsp;// same for Comparable, Hashable, all done in the obvious way &nbsp;&nbsp;&nbsp;<br class=""><br class="">&nbsp;&nbsp;// there’s a lot of utility in synthesizing something like this,<br class="">&nbsp;&nbsp;// &nbsp;I can expand on it if necessary:<br class="">&nbsp;&nbsp;extension SectionIndex: ValueWrapperType {<br class="">&nbsp;&nbsp;&nbsp;&nbsp;typealias WrappedType = Int<br class="">&nbsp;&nbsp;}<br class=""><br class="">...where each method/init/subscript/etc in the derived protocols gets synthesized at compile-time, if not explicitly implemented; similarly, if not explicitly-declared, the derived protocols' typealiases can be synthesized in obvious ways, and it seems acceptable to simply fail to compile (and inform the user of the need to make an explicit-declaration) in cases where such synthesis is impossible.<br class=""><br class="">I think this enough to sketch the way the feature would look and how it would work.<span class="Apple-converted-space">&nbsp;</span><br class=""></blockquote><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">I’m not sure what work is being done by “wrapping X as Y” here; it seems like just another way of expressing a stored property.</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">I think we’re all interested in a “deriving” proposal. &nbsp;However, the key problem that a serious proposal would have to address is not picking the syntax, but describing how derivation would actually work. &nbsp;We’d prefer not to just hard-code rules in the compiler for specific protocols.</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">For example, derivation presumably involves recursively invoking the given operation on each of the stored properties (what does “on” mean? which parameters are changed, and which are passed through?) and then merging the results (how?).</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">John.</span></div></div></blockquote></div><br class=""><div class=""><div class="">Apologies for leaving too much out.</div><div class=""><br class=""></div><div class="">I meant to propose that the `deriving` in this place would enforce the wrapper type only wrapped a single stored value, warranting the distinct syntax; I seem to have edited-out both an explicit statement that this assumed a single-stored-property and to have omitted a comment in the `//declaration can continue in here` that no additional stored-properties could be declared (analogous to the rules current applied within extensions).</div><div class=""><br class=""></div><div class=""><div class="">Yes, constraining a `deriving` construct to only support wrappers containing a single stored property would, on the one hand, be somewhat limiting, but on the other hand it would seemingly allow trivial solutions to the issues you bring up:</div><div class=""><br class=""></div><div class="">- `on` is unambiguous as there’s only one thing it can be “on"</div><div class="">- there’s no ordering-of-operations to have to worry about</div><div class="">- there’s no merging-of-results to have to worry about</div><div class="">- i’m guessing there’s no parameters needing to getting changed (but I’m not 100% on what you mean here)</div><div class="">- there’s no associated-type incoherency to worry about (unless user error introduces it)</div><div class=""><br class=""></div><div class="">…there’s least one tricky case (if you want the wrapper to replace one of the wrapped type’s typealiases with a wrapper).</div><div class=""><br class=""></div><div class="">…and at least for me, there’s enough value in that simplified wrapper-synthesis / deriving-type construct to take the time to check community interest.&nbsp;</div><div class=""><br class=""></div><div class="">Thanks for taking the time to read and send feedback.</div><div class=""><br class=""></div><div class="">PS:</div><div class=""><br class=""></div><div class=""><div class="">On the other hand, if this becomes writable:</div><div class=""><br class=""></div><div class="">&nbsp; &nbsp; protocol WrapperType {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp;typealias WrappedValue</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp;var wrappedValue: { get }</div><div class="">&nbsp; &nbsp; }</div><div class=""><br class=""></div><div class="">&nbsp; &nbsp; extension WrapperType : Equatable where WrappedValue: Equatable {</div><div class="">&nbsp; &nbsp; }</div><div class=""><br class=""></div><div class="">&nbsp; &nbsp; func ==&lt;W:WrapperType where W.WrappedValue:Equatable&gt;(lhs: W, rhs: W) -&gt; Bool {</div><div class="">&nbsp; &nbsp; &nbsp; return lhs.wrappedValue == rhs.wrappedValue</div><div class="">&nbsp; &nbsp; }</div><div class=""><br class=""></div><div class="">…etc., then it’s possible (albeit moderately unpleasant) to just write suitable glue logic out longhand on an as-needed basis (and with the caveat that all types wrapping T would potentially adopt all of T’s protocols even when potentially undesirable).</div></div></div></div><div class=""><br class=""></div><div class=""><br class=""></div></body></html>