Swift: NSString to String bridging lost when using generic infix operator
| Originator: | wirth_caesar | ||
| Number: | rdar://18523286 | Date Originated: | 02-Oct-2014 |
| Status: | Closed (Resolved) | Resolved: | |
| Product: | Swift | Product Version: | Xcode 6.1 GM Seed |
| Classification: | Bug | Reproducible: | Always |
Summary:
Many times, when using NSString and String, they can be used pretty much interchangeably. However, when putting them in infix functions, some of this implicit bridging is lost, and compiler errors are thrown.
The code produced below illustrates the problem.
Sample Code:
func getMaybeString() -> String? {
return "awesome"
}
infix operator =?= {}
func =?=<A>(inout lhs: A, rhs: A?) {
lhs = rhs ?? lhs
}
func setOptional<A>(inout lhs: A, rhs: A?) {
lhs = rhs ?? lhs
}
var nsstring: NSString = ""
// Works as expected
// nsstring is now "awesome"
nsstring = getMaybeString() ?? nsstring
nsstring = ""
// Works as expected,
// nsstring now equals "awesome"
setOptional(&nsstring, getMaybeString())
nsstring = ""
// error: 'String' is not identical to 'NSString'
nsstring =?= getMaybeString()
Expected Results:
The infix operator `=?=` is basically used as an 'optional setter' that only sets the value on the LHS to the RHS's value's unwrapped value if it exists.
Unwrapping function and doing it manually works.
Creating another, equivalent normal (not infix) function works.
I would expect the infix function to work just the same.
Actual Results:
It throws a compiler error at me.
'String' is not identical to 'NSString'
Version:
Version 6.1 (6A1042b) (GM Seed)
Version 6.1 (6A1030) (Beta 2?)
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!