Skip to content

Instantly share code, notes, and snippets.

@tmspzz
Last active February 1, 2018 21:37
Show Gist options
  • Save tmspzz/53f43a767e96eb16ff7176f2e8000af1 to your computer and use it in GitHub Desktop.
Save tmspzz/53f43a767e96eb16ff7176f2e8000af1 to your computer and use it in GitHub Desktop.
Decodable Conformance of Enum backed with RawType Struct conforming to ExpressibleByStringLiteral
import Foundation
public struct Bar: Codable {
let value: String
public init(bar: Bar) {
self.value = bar.value
}
}
extension Bar: ExpressibleByStringLiteral {
public typealias StringLiteralType = String
public init(unicodeScalarLiteral value: UnicodeScalar) {
self.value = "\(value)"
}
public init(extendedGraphemeClusterLiteral value: String) {
self.value = value
}
public init(stringLiteral value: String) {
self.value = value
}
}
enum Foo: Bar, Codable { // Change RawType to String, everything is fine
case foo = "Foo"
}
struct Baz: Codable {
let f: Foo
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment