I've two different versions of a data structure, and there are bunch of methods that work against a list of these data structures as shown below,
class NewFooRunner {
public double runFoos(List<Foo> foos) {
// expecting new foos
}
}
List<Foo> oldFoos = new ArrayList<>();
List<Foo> newFoos = new ArrayList<>();
class Foo {
private double someNum;
}
Here NewFooRunner takes a list of Foo, and it's very easy to pass in oldFoos instead of newFoos. I wonder if there's something in the type system to avoid that. In languages with type aliasing, I could've used that. I can create a concrete class to represent OldFoos and NewFoos which holds the list internally like below.
class NewFoos {
private List<Foo> newFoos
}
class NewFooRunner {
public double runFoos(List<NewFoos> foos) {
// expecting new foos
}
}
Is that the only way to approach it?
OldFooandNewFootreated differently, then instantiate them as different classes with no common parent.OldFooandNewFootreated differently sometimes and equivalently other times, then instantiate them as different classes with a common parent.OldFooandNewFootreated equivalently, then instantiate them as the same class.That's what the type system provides.