Files
swift-mirror/stdlib/unittest/LifetimeTracked.swift
Chris Lattner cb8a65e831 Clean up the semantics of 'let' properties in a number of ways:
- 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
2014-12-08 20:59:53 +00:00

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
}