[swift-evolution] [Pitch] Make `default` function parameter values more transparent

Charles Srstka cocoadev at charlessoft.com
Mon Jun 13 12:12:46 CDT 2016


> On Jun 12, 2016, at 11:27 PM, Charlie Monroe via swift-evolution <swift-evolution at swift.org> wrote:
> 
>> 
>> On Jun 11, 2016, at 3:35 PM, Adrian Zubarev via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> 
>> I just installed the current Swift 3 snapshot to play around with it (last from may crashed my Xcode all the time).
>> 
>> I wanted to re-build a small project with (currently implemented) Swift 3 changes. Basically I had to look up on GitHub what the default value for deinitialize(count:) function was for UnsafeMutablePointer, just because Xcode and the docs can’t tell me that:
>> 
>> /// De-initialize the `count` `Pointee`s starting at `self`, returning
>> /// their memory to an uninitialized state.
>> ///
>> /// - Precondition: The `Pointee`s at `self..<self + count` are
>> ///   initialized.
>> ///
>> /// - Postcondition: The memory is uninitialized.
>> public func deinitialize(count: Int = default)
>> To cut it short:
>> 
>> Could we make default function parameter values more transparent in Swift 3?
>> Why are default parameter values translated to default rather than the actual value?
> I guess that in some cases you don't want the default value to be known, or is irrelevant. Most importantly, it can be a more complex expression - e.g. creating an object and calling something on it:
> 
> private let mySecretNumber = 0x999
> public func deinitialize(count: Int = NSProcessInfo().processorCount + mySecretNumber)
> 
> And that's a pretty example, it can get much nastier. Since mySecretNumber is private, it definitely cannot be exposed.

That was my reaction as well, but this change is already in the pipeline, according to Apple’s published plans.

https://github.com/apple/swift/blob/master/docs/LibraryEvolution.rst#default-argument-expressions <https://github.com/apple/swift/blob/master/docs/LibraryEvolution.rst#default-argument-expressions>

Presumably, once this goes through, it will no longer be possible to use a secret number as a default value.

Charles

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160613/8c1879ff/attachment.html>


More information about the swift-evolution mailing list