Skip to content

Hashable이 무엇이고, Equatable을 왜 상속해야 하는지 설명하시오. #28

Description

@Glsme

Hashable

  • hash값을 제공하는 Protocol이다.
public protocol Hashable : Equatable {
    var hashValue: Int { get }
    func hash(into hasher: inout Hasher)
}

Equatable

  • 등호 연산자(==) 또는 부등호 연산자(!=)를 사용해 동등성을 비교하는 Protocol이다.
  • 구조체 / 클래스 / 열거형에서 Equatable 채택 시 비교가 가능하다.

Equatable을 상속해야 하는 이유

hashValue는 고유값이어야 하므로 이를 식별하기 위해 Equatable 내 == 메서드를 사용해서 고유값인지 확인해주어야 한다.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions