[swift-users] Intended behavior or bug in Dictionary's new subscript(_:default:)?
Jens Persson
jens at bitcycle.com
Fri Jun 16 15:40:27 CDT 2017
// Swift 4, Xcode 9 beta 1, default toolchain
import Foundation
var d1 = [Int : String]()
d1[1, default: .init()].append("a")
d1[2, default: .init()].append("b")
d1[3, default: .init()].append("c")
d1[1, default: .init()].append("d")
print(d1) // [2: "b", 3: "c", 1: "ad"] as expected.
var d2 = [Int : NSMutableString]()
d2[1, default: .init()].append("a")
d2[2, default: .init()].append("b")
d2[3, default: .init()].append("c")
d2[1, default: .init()].append("d")
print(d2) // [:] but why?
I know that NSMutableString is a reference type and String is a value type
and that the default argument is an @autoclosure. I also know that the
newly created NSMutableString instance is just released immediately after
the append call, without being stored and retained in the Dictionary's
storage.
Is this the intended behavior and if so, please let me better understand
how/why.
/Jens
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170616/7fc6e06d/attachment.html>
More information about the swift-users
mailing list