<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Sep 19, 2017, at 5:19 AM, David Zarzycki via swift-dev <<a href="mailto:swift-dev@swift.org" class="">swift-dev@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class="Apple-interchange-newline"><br class=""><blockquote type="cite" class=""><div class="">On Sep 18, 2017, at 17:54, Ben Cohen via swift-dev <<a href="mailto:swift-dev@swift.org" class="">swift-dev@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;"><br class=""><div class=""><br class=""><blockquote type="cite" class=""><div class="">On Sep 13, 2017, at 1:06 PM, David Zarzycki via swift-dev <<a href="mailto:swift-dev@swift.org" class="">swift-dev@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><br class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><blockquote type="cite" class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;">On Sep 13, 2017, at 15:23, Matthew Johnson via swift-dev <<a href="mailto:swift-dev@swift.org" class="">swift-dev@swift.org</a>> wrote:<br class=""><br class=""><br class=""><br class="">Sent from my iPhone<br class=""><br class=""><blockquote type="cite" class="">On Sep 13, 2017, at 11:56 AM, David Zarzycki via swift-dev <<a href="mailto:swift-dev@swift.org" class="">swift-dev@swift.org</a>> wrote:<br class=""><br class=""><br class=""><br class=""><blockquote type="cite" class="">On Sep 13, 2017, at 13:53, David Sweeris <<a href="mailto:davesweeris@mac.com" class="">davesweeris@mac.com</a>> wrote:<br class=""><br class=""><br class=""><blockquote type="cite" class="">On Sep 13, 2017, at 09:54, David Zarzycki via swift-dev <<a href="mailto:swift-dev@swift.org" class="">swift-dev@swift.org</a>> wrote:<br class=""><br class="">Hello,<br class=""><br class="">As a part of a research project that I’m working on, I’ve started bumping into the need for value-type bound protocols (as opposed to the existing class bound protocols). Is this something that would be worth proposing formally? Or should I just keep the patch I have on my research branch?<br class=""></blockquote><br class="">I think it'd be worth a proposal, especially if can talk about why you needed it.<br class=""></blockquote><br class="">While I look forward to talking about my research, I’m not ready to do in the near future.<br class=""><br class="">That being said, value-type bound protocols seem independently useful and that is why I emailed the list.<br class=""><br class="">I think the use case for this is generic algorithms. Why? Because it can be hard to impossible to write *robust* generic code when you don’t know whether an abstract type copies by value or by reference during assignment/initialization. With class-bound protocols, you can guarantee reference semantics, but there is no analogous feature for ensuring value semantics. I have a small (~150 line) patch that fixes this.<br class=""></blockquote><br class="">Value types and value semantics are not the same. Most people who have asked for this capability actually want a constraint for value semantics, not value types. Is that what you're asking for as well?<br class=""></blockquote><br class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;">The patch that I’m ready to put forth is only a value-type bound. In other words only structs and enums would be able to conform to a value-type bound protocol. Enforcing value semantics is arguably a separable language goal.</span><br class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;"></div></blockquote><div class=""><br class=""></div><div class="">But knowing something is a value type isn’t particularly useful, given it doesn’t guarantee value semantics. It could even do more harm than good, by being confusable with enforcing value semantics. </div><div class=""><br class=""></div><div class="">Can you go into the use cases you have where you would use the knowledge that a type is a value type?</div></div></div></div></blockquote></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">Hi Ben,</div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">As a part of a much larger goal, I’m experimenting with enforced value *semantics* and I found that value-type bound protocols are a wholly separable and independently useful prerequisite. Here is a contrived but representative example:</div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><font face="Courier" class="">protocol ValueThingy : !class { // From the patch sent to the list</font></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><font face="Courier" class=""> mutating func increment()</font></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><font face="Courier" class="">}</font></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><font face="Courier" class=""><br class=""></font></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><div class=""><font face="Courier" class="">func incrementByCopy<T : </font><span class="" style="font-family: Courier;">ValueThingy</span><font face="Courier" class="">>(_ arg : T) -> T {</font></div><div class=""><font face="Courier" class=""> var copy = arg</font></div><div class=""><font face="Courier" class=""> copy.increment()</font></div><div class=""><font face="Courier" class=""> return copy</font></div><div class=""><font face="Courier" class="">}</font></div><div class=""><br class=""></div><div class="">Without value-type bound protocols, generic code cannot ensure that required copies are actually happening. This is independently useful and good.</div></div></div></blockquote><br class=""></div><div>As others have noted, this doesn't by itself guarantee anything. Value semantics is a property of an operation, not of a type. You can use an Int as a reference:</div><div><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div>var counters: [Double]</div><div><br class=""></div><div>extension Int: ValueThingy {</div><div> mutating func increment() {</div><div> counters[self] += 1.0</div><div> }</div><div>}</div></blockquote><br class=""><div class="">which, despite "using value types everywhere", violates the contract you're looking for without warning. On the other hand, a class can also serve as a value:</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class="">protocol Incrementable {</div><div class=""> var incremented: Self { get }</div><div class="">}</div><div class=""><br class=""></div><div class="">extension ValueThingy where Self: Incrementable {</div><div class=""> mutating func increment() {</div><div class=""> self = self.incremented</div><div class=""> }</div><div class="">}</div><div class=""><br class=""></div><div class="">class IndirectInt: ValueThingy, Incrementable {</div><div class=""> var value: Int</div><div class=""> init(value: Int) { self.value = value }</div><div class=""><br class=""></div><div class=""> var incremented: Int { return IndirectInt(value: value + 1) }</div><div class="">}</div><div class=""><br class=""></div></blockquote>The more fundamental thing I think we're looking for in this space is a "pure" restriction for functions and methods, meaning they only access non-shared-mutable data. Any annotation at the type level is not going to give strong enough guarantees to build sound abstractions on top of.<div class=""><br class=""></div><div class="">-Joe</div></body></html>