<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 Nov 29, 2017, at 7:27 PM, Dave DeLong via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@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; line-break: after-white-space;" class="">Hi SE,<div class=""><br class=""></div><div class="">I’m pondering some esoteric type stuff as I’m sketching out an improved date/time library (<a href="https://github.com/davedelong/Chronology" class="">https://github.com/davedelong/Chronology</a>), and I’m wondering two things:</div><div class=""><br class=""></div><div class="">1️⃣ Is there a way to express a type that does *not* conform to a protocol? </div><div class=""><br class=""></div><div class="">For example, let’s say I have three protocols:</div><div class=""><br class=""></div><div class=""><font face="Menlo" class=""><span style="font-size: 11px;" class="">protocol YearHaving { var year: Int }</span></font></div><div class=""><font face="Menlo" class=""><span style="font-size: 11px;" class="">protocol MonthHaving { var month: Int }</span></font></div><div class=""><font face="Menlo" class=""><span style="font-size: 11px;" class="">protocol DayHaving { var day: Int }</span></font></div><div class=""><font face="Menlo" class=""><span style="font-size: 11px;" class="">typealias DateHaving = YearHaving & MonthHaving & DayHaving</span></font></div><div class=""><br class=""></div><div class="">Now, I want to allow for some adjustments to values that have these types:</div><div class=""><br class=""></div><div class=""><font face="Menlo" class=""><span style="font-size: 11px;" class="">extension DayHaving {</span></font></div><div class=""><font face="Menlo" class=""><span style="font-size: 11px;" class=""> func removingDay() → Self & ¬DayHaving // whatever type “self” is, except it does not conform to “DayHaving”, because you just removed the day value</span></font></div><div class=""><font face="Menlo" class=""><span style="font-size: 11px;" class="">}</span></font></div><div class=""><br class=""></div><div class="">Is this possible?</div></div></div></blockquote><div><br class=""></div>There is no way to express this.</div><div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div class=""><br class=""></div><div class="">2️⃣ If this is not possible, would it be hard to add something like this in?</div></div></div></blockquote><br class=""></div><div>It would not be hard, although do note that it can only check “there is no conformance of the type to the protocol visible from this module”; a conformance might be present in some other module, or might be loaded dynamically later on. It can also be “hidden” from the static type system:</div><div><br class=""></div><div><font face="Menlo" class="">func f<T>(_: T) where <span style="font-size: 11px;" class="">¬DayHaving { }</span></font></div><div><font face="Menlo" class="">struct X { }</font></div><div><font face="Menlo" class="">f(X()) // okay</font></div><div><font face="Menlo" class="">struct Y : DayHaving { }</font></div><div><font face="Menlo" class="">f(Y()) // error: Y conforms to DayHaving but shouldn’t</font></div><div><font face="Menlo" class=""><br class=""></font></div><div><div><font face="Menlo" class=""><span style="font-size: 11px;" class="">func g<T>(_ t: T) { f(t) } // succeeds, because T is not known to conform to DayHaving</span></font></div><div class=""><font face="Menlo" class=""><span style="font-size: 11px;" class="">g(X()) // okay, as expected</span></font></div><div class=""><font face="Menlo" class=""><span style="font-size: 11px;" class="">g(Y()) // okay, but surprising</span></font></div><div class=""><font face="Menlo" class=""><span style="font-size: 11px;" class=""><br class=""></span></font></div></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>- Doug</div><div><br class=""></div><br class=""></body></html>