Is there a way to achieve a mixin in Ruby or a trait in Scala in F#?
What i want is to basically copy one module into another so that it shares the other modules functionality but is closed for modification. Or, an OOP way of thinking about it, I want multiple inheritance except that the parent object can not be modified.
You can abuse
inlineand member constraints to do duck typing, which gets you some of the benefits of mixins. For example, you could translate this Ruby code (taken from this tutorial):to this:
It has the (arguable) advantage over extension methods of not requiring a common base class or interface. Regarding your previous question about the
openkeyword, you could have several modules definingwhoAmIand the oneopened last will shadow previous ones. In that way you can sort of “mix in” which module you want. The F# core library uses a similar approach with checked operators.