Un oggetto immutabile non può essere modificato dopo la costruzione — ogni campo viene impostato una volta sola, e non ci sono metodi che mutano lo stato. Per "modificarlo", si crea un nuovo oggetto.
Costruire un tipo immutabile
{
cents;
String currency;
{
.cents = cents;
.currency = currency;
}
Money {
(.cents + other.cents, currency);
}
{ cents; }
}
