I want to do something similar to Scala's wildcards for pattern matching,
so I need to instantiate an object of type T
that will always return true
when calling the equals()
- method.
I know this is kind of hacky, so if I would attempt to call any other function on this object my computer might as well burst into flames, the only thing that is important is equals()
.
What I have so far:
public void match(Object toMatch, Effect0 effect) {
val bothNull = toMatch == null && value == null;
val equals = toMatch != null && toMatch.equals(value);
if(bothNull || equals) {
effect.f();
}
}
public static Object any() {
return new Object() {
@Override
public boolean equals(Object o) {
return true;
}
};
}
But I have to somehow lift any()
into type T
.
The usage would look like this:
myClass.match(new MyClass(any(), "foo", "bar", any()), () -> ...);
What's important is that I can't, for example, compare a Pair<String, Integer>
with a Pair<Integer, String>
. So that's why I need generics.
Is that possible?