Nothing made this as clear for me as using the Nim language, where functions defined as
proc doThing(x: MyObject; value: string)
can be called both with doThing(x, y)
and with x.doThing(y)
where x by itself is 'just' a data object, but is treated transparently by the language as having associated functions, setters, getters, etc as if it were a class (even though they can all also be directly called separately).It's an approach that I find really elegant and that I wish more languages used in some way.