Scala newbie here. I want to create a class/trait for error correction layers that has methods with no mundated implementation (e.g. a function for output error probability given input error probability that might be completely different for each instance) but also a composition operator that is the same for every instance of the class. What is the way of doing this with classes and traits, given that what I want is somewhere inbetween the two.
In the end I would like to have something like this:
trait ECC(duration: Int, space: Int) {
def output_error_rate(input_error_rate: Int): Int
def <(other: ECC) : ECC = {
val output = new ECC (duration + other.duration, space * other.space)
output.output_error_rate(input_error_rate) = output_error_rate(other.output_error_rate(input_error_rate)
return output
}
}
class PerfectECC(duration: Int, space: Int) instance of ECC {
def output_error_rate(a: Int): Int = {
return 35 * a * a
}
}
From a pure syntax point of view, what you want can already be implemented:
trait ECC(val duration: Int, val space: Int) {
def output_error_rate(input_error_rate: Int): Int
def <(other: ECC) : ECC = {
val that = this
val output = new ECC (duration + other.duration, space * other.space) {
def output_error_rate(input_error_rate: Int) = that.output_error_rate(other.output_error_rate(input_error_rate))
}
output
}
}
class PerfectECC(duration: Int, space: Int) extends ECC(duration, space) {
def output_error_rate(a: Int): Int = {
return 35 * a * a
}
}
val p1 = PerfectECC(1,2)
val p2 = PerfectECC(3,4)
p2.output_error_rate(5) // 875
p1.output_error_rate(875) // 26796875
p1.<(p2).output_error_rate(5) // 26796875
Though to be honest, it's a bit unclear if this really is the best approach to solve your problem.