[swift-evolution] [Proposal] autocreate parameter for optional values

Kwanghoon Choi eyerama at gmail.com
Thu Jul 28 21:08:40 CDT 2016


Dear swifters.

I have an proposal for autocreation of optional variables

This idea came from below situation

I have to addtional header to NSURLSessionConfiguration like this

let config = NSURLSessionConfiguration.defaultSessionConfiguration()
config.HTTPAdditionalHeaders?["Some-Additional-Info"] = "1992-08-01"

here is the problem

how to be sure not nil for HTTPAdditionalHeaders in
defaultSessionConfiguration

may be nil or not nil, I read api reference of HTTPAdditionalHeaders but,
did't see  for value state of creation.

yeah, I know. it's optional. so may be I should thought it must be nil. but
really must be? I think it's not very clear logic.

anyway, HTTPAdditionalHeaders was nil. so i have to solve, and solve like
this.

let config = NSURLSessionConfiguration.defaultSessionConfiguration()
var headers = config.HTTPAdditionalHeaders ?? [NSObject: AnyObject]()
headers["Some-Additional-Info"] = "1992-08-01"

May be another way to solve this.

*so I suggest autocreate parameter or something like that*

NSURLSessionConfiguration have this variable
public var HTTPAdditionalHeaders: [NSObject : AnyObject]?

*change above things to like below*

public *autocreate* var HTTPAdditionalHeaders: [NSObject : AnyObject]? {

*create* { return [NSObject: AnyObject]() }

}


let headers = config.HTTPAdditionalHeaders must be return nil

but

config.HTTPAdditionalHeaders["SomeThinkConvenient"] = "YEAH~"

this is automatically create Dictionary and set key and value


What about this idea, Dears?


Thank you for reading.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160729/d775e6c8/attachment.html>


More information about the swift-evolution mailing list