[swift-evolution] [Pitch/Reality Check] Allow instance members as parameter default values
Kevin Nattinger
swift at nattinger.net
Wed Feb 22 23:03:22 CST 2017
> On Feb 22, 2017, at 8:25 PM, Derrick Ho via swift-evolution <swift-evolution at swift.org> wrote:
>
> Did you know you can use a static variable as a default parameter?
>
> class poncho {
> static var color= "yellow"
>
> func foo(color: String = poncho.color){}
> }
>
> I think instance members should not be allowed as default members though. It may lead to unexpected behavior.
Could you explain or give an example of how the behavior would be unexpected?
> On Wed, Feb 22, 2017 at 11:06 PM T.J. Usiyan via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
> +1 if this doesn't have terrible implications.
>
> The downside to making it IUO or Optional is that that changes the type signature of `index(i:offsetBy:)` for no reason other than a default value. If you decide to change to or away from providing a default value it probably shouldn't change the type accepted in that spot.
>
> On Wed, Feb 22, 2017 at 7:32 PM, Ben Cohen via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>
>> On Feb 22, 2017, at 10:42 AM, Nate Cook via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>
>> Oops, left out that there's this horrifying way of writing it right now:
>>
>> extension Collection {
>> func index(_ i: Index! = nil, offsetBy n: IndexDistance) -> Index {
>> let i = i ?? startIndex
>> // ...
>> }
>> }
>>
>> Nobody wants that.
>>
>
> Oh I don’t think it’s all that bad! It also doesn’t need to be an IUO, since you’re unwrapping it immediately into another variable no matter what.
>
> This also gives you the flexibility to write this:
>
> extension Collection {
> func index(_ i: Index? = nil, offsetBy n: IndexDistance) -> Index {
> let i = i ?? (n < 0 ? endIndex : startIndex)
> // ...
> }
> }
>
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
>
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
>
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170222/44f20078/attachment.html>
More information about the swift-evolution
mailing list