nmdias / DefaultsKit

Simple, Strongly Typed UserDefaults for iOS, macOS and tvOS
MIT License
1.43k stars 95 forks source link

Redundant conformance constraint #7

Closed kmcgill88 closed 7 years ago

kmcgill88 commented 7 years ago

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!

screen shot 2017-10-12 at 1 05 16 pm
nmdias commented 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
kmcgill88 commented 7 years ago

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?

nmdias commented 7 years ago

Yes, all of the above are correct.

kmcgill88 commented 7 years ago

Awesome! Thanks man!