-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We have had two `Box` variants for a very long time: `Box` and `VarBox`, containing an immutable and mutable value respectively. However, from our experience the immutable variant is not used often if at all, which means its immutability guarantee is not something that people want or need. Furthermore, following the latest language developments a `Box` is a very good candidate for a property wrapper, and more so now that they can be defined as local properties. ## Changes - Deprecate `VarBox` in favor of `Box`, making it wrap a mutable value. - Make `Box` a `@propertyWrapper`, and `@dynamicMemberLookup`. The latter is useful if we use `Box<T>` as a regular type (i.e. not a PW).
- Loading branch information
Showing
6 changed files
with
65 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,39 @@ | ||
/// An arbitrary container which stores a **constant** value of type `T`. | ||
/// An arbitrary container (and property wrapper) which stores a **mutable** value of type `T`. | ||
/// | ||
/// This main purpose of this object is to encapsulate value types so that they can be used like a reference type | ||
/// (e.g. pass value types around without copying them, "share" value types between closures, etc) | ||
/// The main purpose of this object is to encapsulate value types so that they can be used like a reference type | ||
/// (e.g. pass value types around without copying them, memoize value types inside closures via capture list, etc) | ||
@propertyWrapper | ||
@dynamicMemberLookup | ||
public final class Box<T> { | ||
|
||
/// The encapsulated value. | ||
public let value: T | ||
/// The wrapped value. | ||
public var wrappedValue: T | ||
|
||
/// Instantiate a new constant value box with the given value. | ||
/// Instantiate a new mutable value box with the given value. | ||
/// | ||
/// - parameter value: the value to encapsulate. | ||
/// - parameter wrappedValue: the value to encapsulate. | ||
/// | ||
/// - returns: a newly instantiated box with the encapsulated value. | ||
public init(_ value: T) { self.value = value } | ||
public init(wrappedValue: T) { self.wrappedValue = wrappedValue } | ||
|
||
public subscript<U>(dynamicMember keyPath: KeyPath<T, U>) -> U { wrappedValue[keyPath: keyPath] } | ||
} | ||
|
||
/// An arbitrary container which stores a **variable** value of type `T`. | ||
/// | ||
/// This main purpose of this object is to encapsulate value types so that they can be used like a reference type | ||
/// (e.g. pass value types around without copying them, "share" value types between closures, etc) | ||
public final class VarBox<T> { | ||
extension Box { | ||
|
||
/// The encapsulated value. | ||
public var value: T | ||
/// The wrapped value (compact). | ||
public var value: T { | ||
get { wrappedValue } | ||
set { wrappedValue = newValue } | ||
} | ||
|
||
/// Instantiate a new variable value box with the given value. | ||
/// Instantiate a new mutable value box with the given value (compact). | ||
/// | ||
/// - parameter value: the value to encapsulate. | ||
/// - parameter wrappedValue: the value to encapsulate. | ||
/// | ||
/// - returns: a newly instantiated box with the encapsulated value. | ||
public init(_ value: T) { self.value = value } | ||
public convenience init(_ value: T) { self.init(wrappedValue: value) } | ||
} | ||
|
||
@available(*, unavailable, renamed: "Box") | ||
public typealias VarBox<T> = Box<T> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters