Skip to content
This repository has been archived by the owner on Nov 15, 2020. It is now read-only.

Refactoring for ModelProtocol protocol #3

Closed
hachinobu opened this issue May 23, 2018 · 2 comments
Closed

Refactoring for ModelProtocol protocol #3

hachinobu opened this issue May 23, 2018 · 2 comments
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@hachinobu
Copy link

Hi, @malcommac

Thank you for providing a nice library.

Please tell me about the implementation of ModelProtocol.

If the object conforms to the Equatable protocol, i can use the default implementation of Extension so i do not need to write isEqual method.

Likewise, if the object conforms to the Hashable protocol, i do not want to write the identifier property.

Is there a problem with the code below?

extension ModelProtocol where Self: Hashable {
	
    public var identifier: Int {
        return self.hashValue
    }
    
    //remove code
//    public var hashValue: Int {
//        return self.identifier.hashValue
//    }
	
}

It seems that compatibility is good.

Synthesizing Equatable and Hashable conformance

I think that implementation will be easier.

best regards.

@malcommac
Copy link
Owner

malcommac commented Aug 22, 2018

Hi, you are correct, ModelProtocol is pretty weird yet.
My initial concern is to force the user to adopt this protocol in order to be conform to Hashable protocol, used internally to perform diffing between two data reloads.
In fact, actually is a mix of bad practices.
I've tried to refactor it by removing both isEqual function and identifier variable.
ModelProtocol right now must implement only a variable, hashValue (the same you need to be Hashable conform, even this is a just a repetion). What do think about it?

public protocol ModelProtocol {
	var hashValue: Int { get }
}

Inside the diff functions are models are therefore casted to AnyHashable, if it fails a debugPrint is printed to the console.

@malcommac malcommac changed the title About the implementation of ModelProtocol Refactoring for ModelProtocol protocol Aug 22, 2018
@malcommac malcommac added the enhancement New feature or request label Aug 22, 2018
@malcommac malcommac added this to the 0.6.0 milestone Aug 22, 2018
@malcommac malcommac self-assigned this Aug 22, 2018
malcommac added a commit that referenced this issue Aug 23, 2018
@malcommac
Copy link
Owner

I've changed partially changed the approach to make it much cleaner.

Each model of the cell must be conform to the ModelProtocol protocol.
This protocol require the implementation of a single property called modelID (an Int) which is used to uniquely identify an object for DeepDiff.

A default implementation of this property is provided via ObjectIdentifier for class-based objects (AnyObject).
Instead an explicit implementation must be provided for value based objects (for example structs).

malcommac added a commit that referenced this issue Aug 23, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants