Code Monkey home page Code Monkey logo

autohashequals.jl's Introduction

Build Status Coverage Status AutoHashEquals

AutoHashEquals

A macro to add == and hash() to composite types (ie type and immutable blocks).

For example:

@auto_hash_equals type Foo
    a::Int
    b
end

becomes

type Foo
    a::Int
    b
end
Base.hash(a::Foo) = hash(a.b, hash(a.a, hash(:Foo)))
==(a::Foo, b::Foo) = isequal(a.b, b.b) && isequal(a.a, b.a) && true

Where

  • we use isequal() because we want to match Inf values, etc.

  • we include the type in the hash so that different types with the same contents don't collide

  • the type and true make it simple to generate code for empty records

  • the Base module is explicitly used in Base.hash so that you don't need to import it

Background

Julia has two composite types: value types, defined with immutable, and record types, defined with type.

Value types are intended for compact, immutable objects. They are stored on the stack, passed by value, and the default hash and equality are based on the literal bits in memory.

Record types are allocated on the heap, are passed by reference, and the default hash and equality are based on the pointer value (the data address).

When you embed a record type in a value type, then the pointer to the record type becomes part of the value type, and so is included in equality and hash.

Given the above, it is often necessary to define hash and equality for composite types. Particularly when record types are used (directly, or in a value type), and when records with the same contents are semantically equal.

A common way to do this is to define the hash as a combination of the hashes of all the fields. Similarly, equality is often defined as equality of all fields.

This macro automates this common approach.

Warning

If you use this macro for a mutable type, then the hash depends on the contents of that type, so changing the contents changes the hash. Such types should not be stored in a hash table (Dict) and then mutated, because the objects will be "lost" (as the hash table assumes that hash is constant).

More generally, this macro is only useful for mutable types when they are used as immutable records.

autohashequals.jl's People

Contributors

andrewcooke avatar

Stargazers

 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.