Myrddin: Traits

Summary: Traits

pkg std =
        trait equatable @a =
                eq      : (a : @a, b : @a -> bool)
        ;;

        trait hashable @a =
                hash    : (a : @a -> uint64)
        ;;

        impl equatable @a[:]
        impl equatable @a::(integral,numeric)
        impl equatable @a#

        impl hashable @a[:]
        impl hashable @a::(integral,numeric)
        impl hashable @a#
;;

Traits: Equatable

trait equatable @a =
        eq      : (a : @a, b : @a -> bool)
;;

The equatable trait allows two values of the type @a to be compared with each other. The function should return true if the values are the same, or false otherwise.

Several default implementations exist, aiming to provide sane equality comparisons for many common cases.

Default Impls

impl equatable @a[:]

The equatable implementation for generic slices does a shallow, byte for byte comparison of each slice element.

impl equatable @a::(integral,numeric)

The equatable implementation for integral types uses the == comparison to check whether two integral values are equal.

impl equatable @a#

The equatable implementation for pointer types uses the == comparison to check whether two integral values are equal. This checks whether the two pointers are equal, and does not concern itself

Traits: Equatable

trait hashable @a =
        hash    : (a : @a -> uint64)
;;

The hashable trait allows the hash of a value to be computed.

Several default implementations of hashing are provided. The specific hash function is implementation defined and subject to change. At the moment, we have chosen siphash as a good default hash function.

Default Impls

impl hashable @a[:]

The hashable impl for slices hashes does a shallow hash of each byte of the slice.

impl hashable @a::(integral,numeric)

The hashable impl for integral types hashes a single integer, scrambling its bits.

impl hashable @a#

The hashable impl for integral types hashes a pointer, scrambling its bits. It does not follow the pointer.

Examples

This example equates a few types which match the equatable trait.

This example hashes a few types which match the hashable trait.

This example adds a new implementation for the equatable trait, and then puts them into a hash table.