Split JavaConverter into two typeclasses to aid type inference
As @mandubian pointed out on Gitter:
class JavaConverter a b where
toJava :: a -> b
fromJava :: b -> a
makes type inference really difficult.
We can split it up and add functional dependencies to make it better:
class ToJava a b | a -> b where
toJava :: a -> b
class FromJava a b | a -> b where
fromJava :: b -> a
Hey! Can I take this one up?
@njayinthehouse Sure! This change is a breaking change. So you have to think through how you are going to implement this properly.
Hey! I'm working on it right now. My approach has been to:
-
Replace the class definition as shown by @rahulmutt above
-
Replace each instance declaration of JavaConverter with an instance declaration of ToJava and FromJava each.
But just out of curiosity, how does a change like this improve type inference?
I had written down the same change, only to realize that declarations like
instance Integral a => ToJava a JInteger where
....
instance Integral a => ToJava a JLong where
....
break FuncDeps.
@njayinthehouse see https://wiki.haskell.org/Functional_dependencies for a explanation on how fundeps can help to compiler to infer the type of a expression
@mbrc12 Has a good point.
Moreover, I recently encountered a very nasty bug in the directory package that uses fromJava in its implementation and ended up performing I/O in the wrong order producing the wrong results. This is definitely not a good thing and it means we need to re-think whether the JavaConverter type class is a good idea in the first place.
When converting from a pure immutable Eta type, the conversion process will be side-effect free in most cases. I don't want to say all because it's Java we're talking about and there's definitely going to be an exception.
When converting a mutable Java object to an Eta type, the conversion process can have side-effects and can also fail in case there's a domain mismatch between the source and target.
So the real type signatures for ToJava and FromJava that handles all this is:
class ToJava a b where
toJava :: a -> Java c (Maybe b)
class FromJava a b where
fromJava :: b -> Java c (Maybe a)
Now it makes conversions harder to use, but makes sure we handle all the possible error cases that can happen when transitioning from Java to Eta and vice-versa.
I'm removing the low-hanging-fruit label from this issue since it's not simple anymore.
I must confess, this bug is a little above my ability to fix right now. Perhaps I'll get to this again eventually (unless someone fixes it before then), but I think I'll try another one for now, if that's okay.
On Sat, Feb 24, 2018, 10:45 PM Rahul Muttineni [email protected] wrote:
@mbrc12 https://github.com/mbrc12 Has a good point.
Moreover, I recently encountered a very nasty bug in the directory package that uses fromJava in its implementation and ended up performing I/O in the wrong order producing the wrong results. This is definitely not a good thing and it means we need to re-think whether the JavaConverter type class is a good idea in the first place.
When converting from a pure immutable Eta type, the conversion process will be side-effect free in most cases. I don't want to say all because it's Java we're talking about and there's definitely going to be an exception.
When converting a mutable Java object to an Eta type, the conversion process can have side-effects and can also fail in case there's a domain mismatch between the source and target.
So the real type signatures for ToJava and FromJava that handles all this is:
class ToJava a b where toJava :: a -> Java c (Maybe b)
class FromJava a b where fromJava :: b -> Java c (Maybe a)
Now it makes conversions harder to use, but makes sure we handle all the possible error cases that can happen when transitioning from Java to Eta and vice-versa.
I'm removing the low-hanging-fruit label from this issue since it's not simple anymore.
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/typelead/eta/issues/560#issuecomment-368243597, or mute the thread https://github.com/notifications/unsubscribe-auth/AMKYfcWaEh0u8VsdpvNi4qq-fqb_R38Aks5tYEObgaJpZM4QiLZa .
@rahulmutt maybe we can let maintain the old simple conversion, renaming it to:
class UnsafeToJava a b where
unsafeToJava :: a -> Java c (Maybe b)
class UnsafeFromJava a b where
unsafeFromJava :: Java c a -> b
or create a new one prefixed with safe to Maybe case
@jneira I feel that we can add additional default methods:
class ToJava a b where
toJava :: a -> Java c (Maybe b)
unsafeToJava :: a -> Maybe b
unsafeToJava = unsafePerformJava . toJava
veryUnsafeToJava :: a -> b
veryUnsafeToJava = fromJust . unsafeToJava
class FromJava a b where
fromJava :: b -> Java c (Maybe a)
unsafeFromJava :: b -> Maybe a
unsafeFromJava = unsafePerformJava . fromJava
veryUnsafeFromJava :: b -> a
veryUnsafeFromJava = fromJust . unsafeFromJava
I'm open for suggestions on better naming :)
Yes this will be a breaking change, and I'd rather have the "safe" version be default and the unsafe version have an explicit "unsafe" prefix.
Mmm, but most cases are safe, no? if we create two different type classes and we select the correct one (only unsafe) for the problematic types, the client code will not be able to choose the bad one (using the veryUnsafe method). The error "only" will be when choosing the type class.
In that way it will follow the actual design of ffi imports cause you can choose extract directly the type or use Maybe if you know the java method can return null.
Otoh Maybe dont provide much information, it could be fromJava :: Java c (Either JException a)?
Most Java objects are mutable and only a handful of classes from the standard library (namely java.lang.String) are guaranteed to be immutable. But even then, a lot of Java objects are only "locally mutable" via the builder pattern where you create the object, mutate it for a very brief period and then freeze the object (by not changing it afterwards at all). The JVM ecosystem is like a wilderness and lots of crazy stuff can happen if you're not careful.
After getting bitten by the directory bug, I realized we need to be a lot more careful about these things. I was able to figure that particular bug because I can "see" the laziness when I look at code and realized that out-of-order side effects were causing the problem. It's not so trivial for newcomers and I'd rather not have them face such nasty bugs which could've been prevented had we been more careful with designing the Java-facing part of the standard library.
I like your signature better for fromJava and I agree that Either should be used.