<div dir="ltr">Part of this suggestion seems to be achievable using `guard`. Some, however, isn&#39;t.<div><br></div><div>Folks may be interested in reading about Refinement Types: <a href="https://en.wikipedia.org/wiki/Refinement_(computing)#Refinement_types">https://en.wikipedia.org/wiki/Refinement_(computing)#Refinement_types</a></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Dec 3, 2015 at 2:29 PM, Adrian Kashivskyy <span dir="ltr">&lt;<a href="mailto:adrian.kashivskyy@me.com" target="_blank">adrian.kashivskyy@me.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">How is this different from guard or precondition()?<div><br></div><div>func f(x: Int) {</div><div>    guard (0...1).contains(x) else {</div><div>        return</div><div>    }</div><div>    // use x</div><div>}</div><div><br></div><div>func f(x: Int) {</div><div>    precondition((0...1).contains(x))</div><div>    // use x</div><div>}</div><div><div>
<div style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><font color="#929292"><br>Pozdrawiam – Regards,</font></div><div style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><font color="#929292">Adrian Kashivskyy</font></div>
</div>
<br><div><blockquote type="cite"><div>Wiadomość napisana przez Sean Heber &lt;<a href="mailto:sean@fifthace.com" target="_blank">sean@fifthace.com</a>&gt; w dniu 03.12.2015, o godz. 23:12:</div><div><div class="h5"><br><div><div>It may be nice to have a keyword that tells the compiler to try to statically prove certain conditions are true after some point without needing to leave the scope or wrap it in an if-let or something else that happens at runtime. This way the compiler could check the validity of the assertion and it would do so at compile time.<br><br>For example, imagine using something like this to unwrap an optional beyond a certain point in the function body:<br><br>func doSomething() -&gt; Int {<br>  var something: Int? = nil<br><br>  if something == nil {<br>     something = 42<br>  }<br><br>  require something: Int<br><br>  return something * 2<br>}<br><br>The compiler would be expected to prove that at the “require” line, “something” could be an “Int” (non-optional). If the compiler cannot prove that, it is an error. If it can prove it, the type of the variable is effectively changed from that point on (the compiler effectively changes all instances of “something” to “something!” - but it knows it should never fail which might mean it could avoid doing certain checks and it eliminates a piece of state from the mind of the programmer).<br><br>This idea could be extended in other ways, potentially, if you could specify rules in a flexible enough way. For example, imagine being able to declare a constraint on the ranges of basic value types:<br><br>require x in 0…1<br>let result = someValue * x<br><br>Ideally, the compiler would then attempt to prove that x must always be in the range of 0…1 at this point. If it cannot prove this to be true, then it would be a compiler error.<br><br>Going farther, you could use the same thing on type declarations themselves so you could have a Float that could only contain 0…1:<br><br>var scaler: Float in 0…1<br><br>Or build a function that has constrained input ranges:<br><br>func scale(x: Float in 0…1) -&gt; Float {<br>  return someValue * x<br>}<br><br>I like the idea of this always being a compile-time check when possible, but it could also be useful to force the requirement to be evaluated at runtime by adding a force operator to it:<br><br>func scale(x: Float in! 0…1) -&gt; Float {<br>  return someValue * x<br>}<br><br>In this scenario the compiler would probably still try to prove it statically - and if it could, it’d skip runtime checks - but if it cannot, it would then insert runtime guards that catch requirement failures and crash as necessary - similar to a forced unwrap - instead of being a compile time error.<br><br>l8r<br>Sean<br><br>_______________________________________________<br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br></div></div></div></div></blockquote></div><br></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=P-2BsYbBZHRBuLDBJaL4DIKDNfkkjpROowTyRAObV11qyh4wD911-2BaPOEWmkmpQ3oZJ1CUUDDpL0psf-2BmO6lYKfHIfKk1NP4JOVPMOzP02t0ED4PvU5eUkQU7VBv2i-2Ba4jbNIoqZXfxsobFemloJFVBNdIYaO8SmSKY14TWIrGVVyrPKx2bdbXkeB5mjvcqliAimfRlKZ1uc2Eb69Ufo5fj5MuwE-2BKS-2B-2FzchxHFHODhK8-3D" alt="" width="1" height="1" border="0" style="min-height:1px!important;width:1px!important;border-width:0!important;margin-top:0!important;margin-bottom:0!important;margin-right:0!important;margin-left:0!important;padding-top:0!important;padding-bottom:0!important;padding-right:0!important;padding-left:0!important">
</div>
<br>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br></blockquote></div><br></div></div>