Scala Self-Type Annotations for Constrained Orthogonality
Join the DZone community and get the full member experience.
Join For FreeI talked about orthogonality in design in one of my earlier posts. We had a class Address
in Scala and we saw how we can combine it with other orthogonal
concerns without polluting the core abstraction. We could do this
because Scala offers a host of capabilities to compose smaller
abstractions and build larger wholes out of them. A language is
orthogonal when it allows such capabilities of composition without
overlaps in functionalities between the composing featuresets.
The
design of Scala offers many orthogonal features - I showed some of them
in my earlier post. The power of mixins for adding orthogonal features
..
val a = new Address(..) with LabelMaker {
override def toLabel = {
//..
}
}
and the power of Scala views with implicits ..
object Address {
implicit def AddressToLabelMaker(addr: Address) = new LabelMaker {
def toLabel =
//..
}
}
Here Address and LabelMaker
are completely unrelated and offers truly orthogonal capabilities when
mixed in. However there can be some cases where the mixins themselves are not completely orthogonal
to the core abstractions, but really optional extensions to them. In
fact the mixins implement some functionalities that may depend on the
core abstraction as well. Let's see yet another feature of the Scala
type system that makes this modeling wholesome.
Consider the following abstraction for a security trade that takes place in a stock exchange ..
// details ellided for clarity
case class Trade(refNo: String, account: String,
instrument: String, quantity: Int,
unitPrice: Int) {
// principal value of the trade
def principal = quantity * unitPrice
}
For
every trade executed on the exchange we need to have a set of tax and
fees associated with it. The exact set of tax and fees depend on a
number of factors like type of trade, instruments traded, the exchange
where it takes place etc. Let's have a couple of tax/fee traits that
model this behavior ..
trait Tax {
def calculateTax = //..
}
trait Commission {
def calculateCommission = //..
}
In the above definitions both methods calculateTax and calculateCommission depends on the trade being executed. One option is to keep them abstract in the above trait and provide their implementations after mixing in with Trade ..
val t = new Trade(..) with Tax with Commission {
// implementations
def calculateTax = principal * 0.2
def calculateCommission = principal * 0.15
}
I did it at the instance level. You can very well use this idiom at the class level and define ..
class RichTrade extends Trade with Tax with Commission {
//..
}
However the above composition does not clearly bring out the fact that the domain rules mandate that the abstractions Tax and Commission should be constrained to be used with the Trade abstraction only.
Scala offers one way of making this knowledge explicit at the type level .. using self-type annotations ..
trait Tax { this: Trade =>
// refers to principal of trade
def calculateTax = principal * 0.2
}
trait Commission { this: Trade =>
// refers to principal of trade
def calculateCommission = principal * 0.15
}
The traits are still decoupled. But using Scala's self type annotations you make it explicit that Tax and Commission are meant to be used *only* by mixing them with Trade.
val t = new Trade(..) with Tax with Commission
t.calculateTax
t.calculateCommission
Can I call this constraining the orthogonality of abstractions ? Tax and Commission provide orthogonal attributes to Trade optionally and publish their constraints explicitly in their definitions. It's not much of a difference from the earlier implementations. But I prefer to use this style to make abstractions closer to what the domain speaks.
Opinions expressed by DZone contributors are their own.
Comments