Closed kmcgill88 closed 7 years ago
Hi @kmcgill88,
The generic Type of Key ValueType
, or T
in your example, is already a Codable:
public final class Key<ValueType: Codable> { }
So it's redundant to specify it. Try writing this:
func save<T>(_ object: T, key: Key<T>) { }
And it should automatically infer the save
method signature as:
func save<T>(_ object: T, key: Key<T>) where T : Decodable, T : Encodable
Ah, that did the trick! Thank you! I don't quite understand how it is inferred though.
I did see ValueType
is scoped to Codable
in your code, so in my code, key: Key<T>
, is it accurate to say T
is inferred to be Codable
because thats what your Key
type requires?
Further, since I'm reusing T
, _ object: T
, it also has the inference because T
is in the same argument scope
? If I changed to _ object: U
then I would have to scope U
to be Codable
.
Am I following correctly?
Yes, all of the above are correct.
Awesome! Thanks man!
Great library!
I am trying to write a small wrapper and am running into a compiler warning
Redundant conformance constraint
. I'm newer to generics but was hoping for some direction what I'm doing wrong. Thanks!