GithubHelp home page GithubHelp logo

structequalhash.jl's Introduction

StructEqualHash.jl

This is a Julia package to define equality and hash for structs. It is similar to AutoHashEquals.jl, StructEquality.jl and StructHelpers.jl. It aims to be lightweight and does not use @generated code.

Usage

The macro call

@struct_equal_hash T [fields]

generates definitions for ==, isequal and hash for the struct T,

    function x::T == y::T
    function isequal(x::T, y::T)
    function hash(x::T, h0::UInt)

The equality tests are applied, using short-circuit logic, to the fields of T specified by the tuple fields. The default for fields is all fields of T in the order in which they have been declared. The hash is similarly computed over the type name T as well as over the fields given by fields. Empty structs are allowed.

The type can be a UnionAll type like T{P} where P. In this case the definitions are

    function x::T{P} == y::T{P} where P
    function isequal(x::T{P}, y::T{P}) where P
    function hash(x::T{P}, h0::UInt) where P

Here the methods for == and isequal only apply if x and y are of the same type T{P} (for the same P). If you do not want this for a parametric type, you can simply use the form without parameters.

Examples

julia> struct T{P}; x::Int; y::P end

julia> @struct_equal_hash T

julia> @struct_equal_hash T{Char} (:x,)

julia> @struct_equal_hash T{P} where P <: Number (:y,)

julia> T(1, "a") == T(1, "b")   # method for T
false

julia> T(1, [1, 2]) == T(1, [1.0, 2.0])   # method for T
true

julia> T(1, 'a') == T(1, 'b')   # method for T{Char}
true

julia> T(1, 1) == T(2, 1)       # method for T{P} where P <: Number
true

Types with several parameters

If T has two parameters, then

    @struct_equal_hash T{P,Q} where {P,Q}

defines methods where the types P and Q must agree for the two arguments of == and isequal. If you only want the first types to agree, you can say

    @struct_equal_hash T{P,Q where Q} where P

or, equivalently,

    @struct_equal_hash T{P} where P

If you only want the second types to agree, you can say

    @struct_equal_hash T{P where P,Q} where Q

If both types may differ, you can say

    @struct_equal_hash T{P where P,Q where Q}

or again omit the parameters.

structequalhash.jl's People

Contributors

matthias314 avatar

Stargazers

Orestis Ousoultzoglou avatar Jeffrey Sarnoff avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.