Is it possible to swap the arguments of a constructor? Consider the following example:
case class Foo(a:Int, b:Int) {
if (a > b) {
val tmp = a
a = b
b = tmp
}
}
The compiler throws an error because I reassign to val a
at line 4 which is perfectly fine. However, I need immutable objects. Therefore, declaring a
and b
as variables is not an option.
Is there a known pattern how to solve this problem?