Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@TaLinh
Created May 8, 2023 10:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TaLinh/8890f5d2d80dbf5312e6745dd488dc2a to your computer and use it in GitHub Desktop.
Save TaLinh/8890f5d2d80dbf5312e6745dd488dc2a to your computer and use it in GitHub Desktop.
struct MultiTypeDictionary<Root> {
private var dict: [PartialKeyPath<Root>: Any] = [:]
// Called for non-optional properties
subscript<Value>(keyPath: KeyPath<Root, Value>) -> Value? {
get { dict[keyPath] as? Value }
set { dict[keyPath] = newValue }
}
// Called for optional properties
subscript<Value>(keyPath: KeyPath<Root, Value?>) -> Value? {
get { dict[keyPath] as? Value }
set { dict[keyPath] = newValue }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment