[Proposal] autocreate parameter for optional values

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.

If the compiler can figure out the type of the expression, you can use [:] in place of [NSObject: AnyObject], as below:

let config = URLSessionConfiguration.default
var headers = config.httpAdditionalHeaders ?? [:]
headers["Some-Additional-Info"] = "1992-08-01"

Saagar Jha

ยทยทยท

On Jul 28, 2016, at 19:08, Kwanghoon Choi via swift-evolution <swift-evolution@swift.org> wrote:

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.

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution