Automatic derivable implementations of standard basic methods.

This module introduce Derivable as the main interface to implement (or auto-implement) and provides additional mixin-interfaces with specific default behavior of standard basic methods based on the services of this interface.

The name deriving is inspired from the deriving mechanism of Haskell.

This module also introduce a new annotation auto_derive. See Derivable for details.

Introduced classes

interface Derivable

deriving :: Derivable

Interface of objects that expose some kind of internal representation in a very unreliable way.
interface DeriveEqual

deriving :: DeriveEqual

Implementation of == and hash for Derivable objects.
interface DeriveToS

deriving :: DeriveToS

Implementation of to_s for Derivable objects.

All class definitions

interface Derivable

deriving $ Derivable

Interface of objects that expose some kind of internal representation in a very unreliable way.
interface DeriveEqual

deriving $ DeriveEqual

Implementation of == and hash for Derivable objects.
interface DeriveToS

deriving $ DeriveToS

Implementation of to_s for Derivable objects.
package_diagram deriving::deriving deriving core core deriving::deriving->core a_star-m a_star-m a_star-m->deriving::deriving

Ancestors

module abstract_collection

core :: abstract_collection

Abstract collection classes and services.
module abstract_text

core :: abstract_text

Abstract class for manipulation of sequences of characters
module array

core :: array

This module introduces the standard array structure.
module bitset

core :: bitset

Services to handle BitSet
module bytes

core :: bytes

Services for byte streams and arrays
module circular_array

core :: circular_array

Efficient data structure to access both end of the sequence.
module codec_base

core :: codec_base

Base for codecs to use with streams
module codecs

core :: codecs

Group module for all codec-related manipulations
module collection

core :: collection

This module define several collection classes.
module environ

core :: environ

Access to the environment variables of the process
module error

core :: error

Standard error-management infrastructure.
module exec

core :: exec

Invocation and management of operating system sub-processes.
module file

core :: file

File manipulations (create, read, write, etc.)
module fixed_ints

core :: fixed_ints

Basic integers of fixed-precision
module fixed_ints_text

core :: fixed_ints_text

Text services to complement fixed_ints
module flat

core :: flat

All the array-based text representations
module gc

core :: gc

Access to the Nit internal garbage collection mechanism
module hash_collection

core :: hash_collection

Introduce HashMap and HashSet.
module iso8859_1

core :: iso8859_1

Codec for ISO8859-1 I/O
module kernel

core :: kernel

Most basic classes and methods.
module list

core :: list

This module handle double linked lists
module math

core :: math

Mathematical operations
module native

core :: native

Native structures for text and bytes
module numeric

core :: numeric

Advanced services for Numeric types
module protocol

core :: protocol

module queue

core :: queue

Queuing data structures and wrappers
module range

core :: range

Module for range of discrete objects.
module re

core :: re

Regular expression support for all services based on Pattern
module ropes

core :: ropes

Tree-based representation of a String.
module sorter

core :: sorter

This module contains classes used to compare things and sorts arrays.
module stream

core :: stream

Input and output streams of characters
module text

core :: text

All the classes and methods related to the manipulation of text entities
module time

core :: time

Management of time and dates
module union_find

core :: union_find

union–find algorithm using an efficient disjoint-set data structure
module utf8

core :: utf8

Codec for UTF-8 I/O

Parents

module core

core :: core

Standard classes and methods used by default by Nit programs and libraries.

Children

module a_star-m

a_star-m

# Automatic derivable implementations of standard basic methods.
#
# This module introduce `Derivable` as the main interface to implement (or auto-implement) and
# provides additional mixin-interfaces with specific default behavior of standard basic methods based
# on the services of this interface.
#
# The name *deriving* is inspired from the deriving mechanism of Haskell.
#
# This module also introduce a new annotation `auto_derive`. See `Derivable` for details.
module deriving is
	new_annotation auto_derive
end

# Interface of objects that expose some kind of internal representation in a very unreliable way.
#
# The point of this interface is to allow objects to give a basic representation of
# themselves within a simple key-value dictionary.
# The specific semantic of each key and value is let unspecified.
#
# Moreover the class annotation `auto_derive` will automatically implements the
# interface with the attributes locally defined in the class.
#
# ~~~
# class A
#    auto_derive
#    var an_int: Int
#    var a_string: String
# end
#
# var a = new A(5, "five")
# var map = a.derive_to_map
# assert map.length == 2
# assert map["an_int"] == 5
# assert map["a_string"] == "five"
# ~~~
interface Derivable
	# Returns a map that loosely represents the object `self`.
	#
	# Warning: by default the method returns an empty Map.
	# It is done this way so that subclasses can just call `super` and add their own attributes.
	#
	# Forgetting to redefine `derive_to_map` will broke the expectation of the user of the class
	# Since an empty map is not POLA.
	#
	# Note that the semantic of keys and values is let unspecified.
	# Moreover, there is no specification nor mechanism to avoid key collision.
	fun derive_to_map: Map[String, nullable Object]
	do
		return new HashMap[String, nullable Object]
	end
end

# Implementation of `to_s` for `Derivable` objects.
#
# The implementation uses `to_s` for each value of `attributes_to_map`.
#
# ~~~
# class A
#    auto_derive
#    super DeriveToS
#    var an_int: Int
#    var a_string: String
# end
#
# var a = new A(5, "five")
# assert a.to_s == "an_int:5; a_string:five"
# ~~~
#
# Warning: the method may go in an infinite recursion if there is a circuit in
# the implementation of `to_s`.
interface DeriveToS
	super Derivable
	redef fun to_s do return derive_to_map.join("; ", ":")
end

# Implementation of `==` and `hash` for `Derivable` objects.
#
# The implementations just call `==` and `hash` on `derive_to_map`.
#
# ~~~
# class A
#    auto_derive
#    super DeriveEqual
#    var an_int: Int
#    var a_string: String
# end
#
# var a = new A(5, "five")
# var b = new A(5, "five")
# var c = new A(6, "six")
# assert a == b
# assert a.hash == b.hash
# assert a != c
# ~~~
#
# Warning: the method may go in an infinite recursion if there is a circuit in
# the implementation of `==` or `hash`.
interface DeriveEqual
	super Derivable
	redef fun ==(other) do
		if not other isa Derivable then return false
		return derive_to_map == other.derive_to_map
	end
	redef fun hash do
		return derive_to_map.hash
	end
end
lib/deriving/deriving.nit:11,1--117,3