I stumbled upon a little problem today. Consider a little wrapper class:
class Event<T> {
T value;
Class<T> type;
// other fields, getters and setters omitted for brevity
}
Now I was in a situation where I wanted to convert a Event<Long> into a Event<String> while preserving the other fields and updating the type member.
Eventually I ended up with the most simple "solution":
Event<String> new = new Event(old.getValue().toString(), String.class, other, fields);
Having worked with Haskell on my pet projects however, I naturally longed for a function like fmap :: Functor f => (a -> b) -> f a -> f b (read: given a function from a to b and a functor containing something of type a give me a result containing the b) and after finding no standard implementation I set out to write one myself:
interface Functor<T> {
Functor<S> fmap( Func1<T,S> f );
}
// ... in Event<T>:
Functor<S> fmap( Func1<T,S> f ) {
S newValue = f.call(this.value);
return new Event( newValue, newValue.getClass(), other, fields);
}
Now there is a problem with this solution: after the call to fmap in Java I am left with an instance of type Functor<String> while the same function in Haskell would return a Event<String>.
Is there a way to get my Event back (without unsafely casting it)?
fmapin theEventclass toEvent<S>. SinceEventis a subclass ofFunctorthis should work. - SamYonnou