I’ve become fond of the idea of TDD and started implementing a few ideas in Julia. The weird part about it is that most material is focused on class-based OOP ideas implemented in Java. This ends up creating a bit of confusion when trying to translate the ideas and steps taken by the authors.
Q1: do you have any recommended literature on TDD+Julia from experienced programmers?
Specifically, I’ve been reading Test Driven Development By Example by Kent Beck. By page 28, he has three classes: Money, Dollar and Franc as follows (code will speak better than me):
class Money{
protected int amount;
}
class Dollar extends Money{
}
class Franc extends Money{
}
The first problem in adaptation is Julia doesn’t really recommend inheritance (and doesn’t support class-based OOP).
My adaptation so far is:
abstract type Money end
struct Dollar <: Money
amount
end
struct Franc <: Money
amount
end
Q2: As far as I understand, there’s no recommended way to remove that duplicated “amount”, is there? Aside from some sort of composition or @forward
macro, as I saw in the big, old thread. (Julia hadn’t even reached 1.0 by then). Composition and inheritance: the Julian way - #59 by favba
Lastly, multiple dispatch seems to end up with a bunch of duplicate code, since there’s no super
or similar. For example, I have these
times(m1::Dollar, multiplier::Int) = Dollar(m1.amount * multiplier)
times(m1::Franc, multiplier::Int) = Franc(m1.amount * multiplier)
equals(m1::Money, m2::Money) = m1.amount == m2.amount
equals
is fine, but times
is getting duplicated since it has to call a specific constructor for that specific type. I’ve read a lot these last days and I found a generic way to implement times:
times(m1::Money, multiplier::Int) = typeof(m1)(m1.amount * multiplier)
From my understanding, there’s no way an object is created as ::Money
, so the typeof(m1)()
instruction will always generate a valid concrete constructor.
so… Q3: is that correct?
I’d love to learn more about “The Julian Way of OOP”. If you reached this point, thank you very much!