<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="">How about something like this instead? Seems a bit more general and perhaps more useful (though with numbers it's not as elegant)?<div class=""><br class=""></div><div class="">var alpha: Double where alpha >= 0.0 && alpha <= 0.0</div><div class="">var user: MNUser where user.isLoggedIn</div><div class="">var name: String where !name.isEmpty</div><div class=""><br class=""></div><div class="">Krystof</div><div class=""><br class=""></div><div><blockquote type="cite" class=""><div class="">On May 18, 2016, at 2:49 PM, Adrian Zubarev 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=""><div id="bloop_customfont" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: 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; margin: 0px;" class="">As you guys brought my idea back to life and I’ve done some effort of digging into the Swifts generic future I can show you some fresh ideas.</div><div id="bloop_customfont" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: 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; margin: 0px;" class=""><br class=""></div><div id="bloop_customfont" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: 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; margin: 0px;" class="">`Refinement Types` could be really handy, but put them aside for a moment.</div><div id="bloop_customfont" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: 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; margin: 0px;" class=""><br class=""></div><div id="bloop_customfont" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: 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; margin: 0px;" class="">Actually we could achieve something like Int<minValue, maxValue> in Swift 3. Take a look at this section here: <a href="https://github.com/apple/swift/blob/master/docs/GenericsManifesto.md#generic-value-parameters" class="">https://github.com/apple/swift/blob/master/docs/GenericsManifesto.md#generic-value-parameters</a></div><div id="bloop_customfont" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: 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; margin: 0px;" class=""><br class=""></div><div id="bloop_customfont" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: 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; margin: 0px;" class="">There is a slightly problem with this, we cannot rewrite all types to be generic with default parameters. That said it would be nice if we could overload types somehow (Int vs. Int<Range>), but I’m not sure if this idea would suit the language like this.</div><div id="bloop_customfont" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: 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; margin: 0px;" class=""><br class=""></div><div id="bloop_customfont" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: 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; margin: 0px;" class="">Anyways we could then have:</div><div id="bloop_customfont" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: 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; margin: 0px;" class=""><br class=""></div><div id="bloop_customfont" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: 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; margin: 0px;" class="">struct Int<_ range: Range<Int>> { … }</div><div id="bloop_customfont" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: 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; margin: 0px;" class=""><br class=""></div><div id="bloop_customfont" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: 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; margin: 0px;" class="">Sadly this makes the type not safe at compile time but only signals the user that at runtime you can’t use any number our of its range.</div><br style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: 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=""><div id="bloop_sign_1463574830069963008" class="bloop_sign" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: 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;"><div style="font-family: helvetica, arial; font-size: 13px;" class="">-- <br class="">Adrian Zubarev<br class="">Sent with Airmail</div></div><br style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: 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=""><p class="airmail_on" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: 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;">Am 18. Mai 2016 bei 14:13:09, Vladimir.S via swift-evolution (<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>) schrieb:</p><blockquote type="cite" class="clean_bq" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: 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;"><span class=""><div class=""><div class=""></div><div class="">> I generally think it’s a cool idea and that it can be useful in minimizing<br class="">> partial functions by requiring that these cases are explicitly handled.<br class=""><br class="">Support this opinion.<br class=""><br class="">Other example where such feature could be useful: some public property in<span class="Apple-converted-space"> </span><br class="">type that can accept values from some interval. For example we have class<span class="Apple-converted-space"> </span><br class="">with transparency property that can be 0.0 ... 0.1:<br class=""><br class="">class MyShape {<br class="">public var transparency: Double = 1.0 // 0.0 ... 0.1<br class="">}<br class=""><br class="">yes, we can use willSet/didSet to check this every time. But if we have a<span class="Apple-converted-space"> </span><br class="">number of such properties, we have a lot of repetitive and boilerplate code:<br class=""><br class="">class C {<br class="">var transparancy : Double = 1.0 {<br class="">didSet { check(transparancy, 0.0...1.0) }<br class="">}<br class=""><br class="">var prop1 : Int = 1 {<br class="">didSet { check(prop1, from: -10...10) }<br class="">}<br class=""><br class="">var prop2 : Int = 1 {<br class="">didSet { check(prop2, from: 0...100) }<br class="">}<br class="">}<br class=""><br class="">, and all will be worse if we also need didSet observers to do some<span class="Apple-converted-space"> </span><br class="">'useful' work here.<br class="">Proposed solution will looks like:<br class=""><br class="">class C {<br class="">var transparancy : Double<0.0...1.0> = 1.0<br class=""><br class="">var prop1 : Int<-10...10> = 1<br class=""><br class="">var prop2 : Int<0...100> = 1<br class="">}<br class=""><br class="">Probably the alternative could be some kind of `where` or `bounded` clause<span class="Apple-converted-space"> </span><br class="">for numeric types and arguments:<br class=""><br class="">class C {<br class="">var transparancy : Double = 1.0 where 0.0...1.0<br class="">//var transparancy : Double bounded 0.0...1.0 = 1.0<br class=""><br class="">var prop1 : Int = 1 where -10...10<br class=""><br class="">var prop2 : Int = 1 where 0.0...1.0<br class=""><br class="">//var prop3 : Float bounded 0.0..<100.0 = 0.0<br class="">}<br class=""><br class=""><br class="">On 18.05.2016 11:30, David Rönnqvist via swift-evolution wrote:<br class="">> It reminds me of "Refinement Types" (see for example [this blog post][1] or<br class="">> [this paper][2]).<br class="">><br class="">> I generally think it’s a cool idea and that it can be useful in minimizing<br class="">> partial functions by requiring that these cases are explicitly handled.<br class="">> For example, highlighting that the following `average` implementation<br class="">> divides by zero when the list is empty:<br class="">><br class="">> func average(numbers: [Int]) -> Int {<br class="">> return sum(numbers) / numbers.count<br class="">> }<br class="">><br class="">><br class="">> and requiring that the empty list case is handled separately:<br class="">><br class="">> func average(numbers: [Int]) -> Int {<br class="">> guard !numbers.isEmpty else { return 0 }<br class="">> return sum(numbers) / numbers.count<br class="">> }<br class="">><br class="">><br class="">> Regards,<br class="">> David<br class="">><br class="">> [1]: <a href="http://goto.ucsd.edu/~rjhala/liquid/haskell/blog/blog/2013/01/01/refinement-types-101.lhs/" class="">http://goto.ucsd.edu/~rjhala/liquid/haskell/blog/blog/2013/01/01/refinement-types-101.lhs/</a><br class="">> <<a href="http://goto.ucsd.edu/%7Erjhala/liquid/haskell/blog/blog/2013/01/01/refinement-types-101.lhs/" class="">http://goto.ucsd.edu/%7Erjhala/liquid/haskell/blog/blog/2013/01/01/refinement-types-101.lhs/</a>><br class="">> [2]: <a href="http://goto.ucsd.edu/~nvazou/refinement_types_for_haskell.pdf" class="">http://goto.ucsd.edu/~nvazou/refinement_types_for_haskell.pdf</a><br class="">> <<a href="http://goto.ucsd.edu/%7Envazou/refinement_types_for_haskell.pdf" class="">http://goto.ucsd.edu/%7Envazou/refinement_types_for_haskell.pdf</a>><br class="">><br class="">><br class="">>> On 11 May 2016, at 20:00, Adrian Zubarev via swift-evolution<br class="">>> <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a> <<a href="mailto:swift-evolution@swift.org" class="">mailto:swift-evolution@swift.org</a>>> wrote:<br class="">>><br class="">>> Okay I’m fine with that for now. If you’d have to decide on some syntax<br class="">>> for such a future, how would it look like? I’m just curious.<br class="">>><br class="">>> I tend to square brackets Double[0.0 … 1.0], because otherwise it might<br class="">>> look like a generic type, but I’m not sure if this type refinement could<br class="">>> be applied to other types as well so we actually would stick to the<br class="">>> generic type syntax here Float<-1.0 … 1.0>.<br class="">>><br class="">>> --<br class="">>> Adrian Zubarev<br class="">>> Sent with Airmail<br class="">>><br class="">>> Am 11. Mai 2016 bei 19:54:09, Matthew Johnson (<a href="mailto:matthew@anandabits.com" class="">matthew@anandabits.com</a><br class="">>> <<a href="mailto:matthew@anandabits.com" class="">mailto:matthew@anandabits.com</a>>) schrieb:<br class="">>><br class="">>>> This is called a refinement type. It would be cool to explore that<br class="">>>> direction in the future but it is definitely well out of scope for Swift 3.<br class="">>>><br class="">>>> Sent from my iPad<br class="">>>><br class="">>>> On May 11, 2016, at 12:45 PM, Adrian Zubarev via swift-evolution<br class="">>>> <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a> <<a href="mailto:swift-evolution@swift.org" class="">mailto:swift-evolution@swift.org</a>>> wrote:<br class="">>>><br class="">>>>> Hello Swift community. I'd like to discuss with you if we need<br class="">>>>> something like this in Swift 3 or any future Swift version.<br class="">>>>><br class="">>>>> As you may know there is no way to constrain a numeric type expect for<br class="">>>>> some scope internal assertion or precodintions which may produce a<br class="">>>>> runtime error if the input value is out of the defined bound.<br class="">>>>><br class="">>>>> func foo(value: Int) {<br class="">>>>> assert(value > 0 && value <= 10)<br class="">>>>><br class="">>>>> // passed<br class="">>>>> }<br class="">>>>><br class="">>>>> How would it be if Swift would allow us to constraint numeric typs with<br class="">>>>> ranges/intervals?<br class="">>>>><br class="">>>>> func newFoo(value: Int<1...10>) {<br class="">>>>> // no need for an assertion any more<br class="">>>>> }<br class="">>>>><br class="">>>>> We could go even further and add more then one range/interval:<br class="">>>>><br class="">>>>> func someFoo(value: Int<0...20, 40...60>) { /* do some work */ }<br class="">>>>><br class="">>>>> Not only integers should have this ability but also floating point<br class="">>>>> types like Double and Float.<br class="">>>>><br class="">>>>> Alternative form might look like this:<br class="">>>>><br class="">>>>> Double[1.0...10.0]<br class="">>>>> Float[0.0...1.0, 10.0...100.0]<br class="">>>>><br class="">>>>> One downside of half opened ranges/intervals is the left side of its<br class="">>>>> set. How do we exclude the left element?<br class="">>>>><br class="">>>>> 1...10 means 1..<11 equals [1, 11)<br class="">>>>><br class="">>>>> But how can we create something like (0.0, 1.0), do we need a strange<br class="">>>>> looking binary operator 0.0>..<1.0?<br class="">>>>><br class="">>>>> What do you think? I'd love to hear any feedback to this.<br class="">>>>><br class="">>>>> --<br class="">>>>> Adrian Zubarev<br class="">>>>> Sent with Airmail<br class="">>>>> _______________________________________________<br class="">>>>> swift-evolution mailing list<br class="">>>>> <a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a> <<a href="mailto:swift-evolution@swift.org" class="">mailto:swift-evolution@swift.org</a>><br class="">>>>> <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">>> _______________________________________________<br class="">>> swift-evolution mailing list<br class="">>> <a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a> <<a href="mailto:swift-evolution@swift.org" class="">mailto:swift-evolution@swift.org</a>><br class="">>> <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">><br class="">><br class="">><br class="">> _______________________________________________<br class="">> swift-evolution mailing list<br class="">> <a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">> <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">><br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></div></span></blockquote><span style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: 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="">_______________________________________________</span><br style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: 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, Arial; font-size: 13px; font-style: normal; font-variant-caps: 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="">swift-evolution mailing list</span><br style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: 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=""><a href="mailto:swift-evolution@swift.org" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: 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="">swift-evolution@swift.org</a><br style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: 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=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: 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="">https://lists.swift.org/mailman/listinfo/swift-evolution</a></div></blockquote></div><br class=""></body></html>