Suppose you have two values x and y such that y should be computed every time x is updated. In Java, everything is an object so I have little choice. The way to do it would be via a class, e.g.
public Data {
private type x, y;
Data(type x) {
this.x = x;
this.y = null;
}
void updateX(type2 val) {
// perform operation to update X
// perform (expensive) operation to update Y
}
void getX() {...}
void getY() {...}
}
In clojure, a direct translation of this might use deftype and defprotocol/definterface. But that's too much for just defining a dependency between two variables. In particular, I have an objection to giving it a name and treating it on par with actual types and protocols.
I know this is what defrecords are for -- when you need to make a class that does not represent an entity in the business domain, but then defrecords are immutable.
Do I have other choices? I had a look at RxClojure because the situation here seems to be "reactive", but that seems to be oriented toward "emit"-ing events rather than e.g. just storing the latest one in an atom.