Standard object methods
The methods that most object types must give: equality, hashing, ordering, a string form, copying and operators. The data ideas behind them are in data-and-values; these pages show how a type implements them.
Concepts
- Data classes
A data class is a class made to hold a fixed set of values, for which the compiler generates the constructor, equality, the hash, the string form and often a copy method.
core - Natural ordering
The natural ordering of a type is the default order that the type defines for its values, such as Java's compareTo or Rust's Ord, which sorts and sorted collections use when no other order is given.
core - Object cloning
Object cloning makes a new object with the same state as an existing one. A shallow copy shares the objects that the fields refer to, and a deep copy copies them too.
core - Operator overloading
Operator overloading lets a type define what operators such as +, == and [] do for its values, so user types such as vectors and money can use the same syntax as numbers.
core - String representation of an object
The string representation of an object is the text that a program gets when it prints the object or joins it to a string, often in two forms: a display form for users and a debug form for programmers.
core - The equality and hashing contract
The equality and hashing contract is the rule that equality must be an equivalence relation and that equal objects must have equal hash codes, which hash tables need to find their keys.
core - Universal object methods
Universal object methods are the small set of methods, such as equality, hash code and string form, that a language gives every object and that its libraries call, with defaults that a type overrides.
core
Tasks
- Copy an object
Make a shallow copy and a deep copy of a Playlist object with a list field, then change the list through the original and show that the shallow copy sees the change and the deep copy does not.
9 languagescore - Implement a string form for a type
Give a Color type a display form such as
9 languagescore - Implement equality for a type
Give a Money class with an amount and a currency value equality and a hash that agrees with it, so that equal values are equal from both sides and a hash set keeps only one of them.
9 languagescore - Implement ordering for a type
Give a Version type with three numbers a natural ordering, with compareTo, __lt__, Ord, IComparable or the spaceship operator, and use it to sort version strings by number and not by text.
9 languagescore - Overload an operator
Give a Vec2 type the operators + and == where the language allows user operators, and add and equals methods where it does not, then sum a list of vectors with them.
9 languagescore