Skip to content

Commit

Permalink
Merge pull request #4 from mattpolzin/type-erased-accessor
Browse files Browse the repository at this point in the history
Add some code doc, add an accessor that gets a type-erased value
  • Loading branch information
mattpolzin authored Nov 5, 2019
2 parents b24fd3b + c4fdbe0 commit 18cd995
Show file tree
Hide file tree
Showing 6 changed files with 538 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Poly.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Pod::Spec.new do |spec|
#

spec.name = "Poly"
spec.version = "2.2.0"
spec.version = "2.3.0"
spec.summary = "A general-purpose library to help represent situations where the type of a value is one of a set of types."

# This description is used to generate tags and improve search results.
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ switch animal {
print(rat)
}
```
Or access a type-erased value:
```swift
let someAnimal: Any = animal.value
```

You might consider making a typealias to make your life easier:
```swift
Expand Down
17 changes: 12 additions & 5 deletions Sources/Poly/Poly+Codable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public struct PolyDecodeNoTypesMatchedError: Swift.Error, CustomDebugStringConve
public var debugDescription: String {
let codingPathString = codingPath
.map { $0.intValue.map(String.init) ?? $0.stringValue }
.joined(separator: "/")
.joined(separator: "/")

let failureStrings = individualTypeFailures.map {
let type = $0.type
Expand All @@ -47,10 +47,17 @@ private func decode<Thing: Decodable>(_ type: Thing.Type, from container: Single
} catch (let err as DecodingError) {
ret = .failure(PolyTypeNotFound(type: type, error: err))
} catch (let err) {
ret = .failure(PolyTypeNotFound(type: type, error: DecodingError.typeMismatch(Thing.self,
.init(codingPath: container.codingPath,
debugDescription: String(describing: err),
underlyingError: err))))
ret = .failure(PolyTypeNotFound(
type: type,
error: DecodingError.typeMismatch(
Thing.self,
.init(
codingPath: container.codingPath,
debugDescription: String(describing: err),
underlyingError: err
)
)
))
}
return ret
}
Expand Down
Loading

0 comments on commit 18cd995

Please sign in to comment.