Skip to content

Commit

Permalink
Use String(reflecting:) vs String(describing:) (#391)
Browse files Browse the repository at this point in the history
Resolves #218.

`String(describing:)` initializer applied to metatypes does not include a module name, which can cause problems if two different types with same name come from different modules.

OTOH `String(reflecting:)` does include module name, which makes these reflection strings slightly longer, but should prevent obscure issues with name collisions from happening.
  • Loading branch information
MaxDesiatov committed Mar 20, 2021
1 parent 8076035 commit bde7de9
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Sources/TokamakCore/Preferences/PreferenceKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ public struct _PreferenceStore {
public func value<Key>(forKey key: Key.Type = Key.self) -> _PreferenceValue<Key>
where Key: PreferenceKey
{
values[String(describing: key)] as? _PreferenceValue<Key>
values[String(reflecting: key)] as? _PreferenceValue<Key>
?? _PreferenceValue(valueList: [Key.defaultValue])
}

public mutating func insert<Key>(_ value: Key.Value, forKey key: Key.Type = Key.self)
where Key: PreferenceKey
{
let previousValues = self.value(forKey: key).valueList
values[String(describing: key)] = _PreferenceValue<Key>(valueList: previousValues + [value])
values[String(reflecting: key)] = _PreferenceValue<Key>(valueList: previousValues + [value])
}

public mutating func merge(with other: Self) {
Expand Down
6 changes: 1 addition & 5 deletions Sources/TokamakCore/Reflection/typeConstructorName.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,5 @@
is returned.
*/
public func typeConstructorName(_ type: Any.Type) -> String {
// FIXME: no idea if this calculation is reliable, but seems to be the only way to get
// a name of a type constructor in runtime. Should definitely check if these are different
// across modules, otherwise can cause problems with views with same names in different
// modules.
String(String(describing: type).prefix { $0 != "<" })
String(String(reflecting: type).prefix { $0 != "<" })
}
2 changes: 1 addition & 1 deletion Sources/TokamakCore/Views/View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public protocol PrimitiveView: View where Body == Never {}
public extension PrimitiveView {
@_spi(TokamakCore)
var body: Never {
neverBody(String(describing: Self.self))
neverBody(String(reflecting: Self.self))
}
}

Expand Down

0 comments on commit bde7de9

Please sign in to comment.