mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
- We switch to a model where let properties may be "initialized", but never reassigned. Specifically, immutable properties in structs/classes may have an init value specified in their declaration (but can then never be reset in any init implementation) or not (in which case they must be initialized exactly once on all paths through every init. This makes a lot more sense for immutability, defines several problems away, and provides a path to supporting things like (rdar://16181314) - We now *never* default initialize an immutable property. Formerly we would default initialize optional let properties to nil, but this isn't actually useful, and allows an error of omission with let properties. This resolves: <rdar://problem/19035287> let properties should only be initializable, not reassignable and possibly other radars. Swift SVN r23779
51 lines
1.4 KiB
Swift
51 lines
1.4 KiB
Swift
//===--- LifetimeTracked.swift --------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See http://swift.org/LICENSE.txt for license information
|
|
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
var trackedCount = 0
|
|
var nextTrackedSerialNumber = 0
|
|
|
|
public final class LifetimeTracked : ForwardIndexType, Printable {
|
|
public init(_ value: Int) {
|
|
++trackedCount
|
|
serialNumber = ++nextTrackedSerialNumber
|
|
self.value = value
|
|
}
|
|
|
|
deinit {
|
|
assert(serialNumber > 0, "double destruction!")
|
|
--trackedCount
|
|
serialNumber = -serialNumber
|
|
}
|
|
|
|
public var description: String {
|
|
assert(serialNumber > 0, "dead Tracked!")
|
|
return value.description
|
|
}
|
|
|
|
/// Returns the next consecutive value after `self`.
|
|
///
|
|
/// Requires: the next value is representable.
|
|
public func successor() -> LifetimeTracked {
|
|
return LifetimeTracked(self.value.successor())
|
|
}
|
|
|
|
public class var instances: Int {
|
|
return trackedCount
|
|
}
|
|
|
|
public let value: Int
|
|
public var serialNumber: Int = 0
|
|
}
|
|
|
|
public func == (x: LifetimeTracked, y: LifetimeTracked) -> Bool {
|
|
return x.value == y.value
|
|
}
|