inline-java icon indicating copy to clipboard operation
inline-java copied to clipboard

Inheritance

Open mitchellwrosen opened this issue 8 years ago • 4 comments

Hi, are there any plans to add support for Java inheritance? From scanning the docs, all I found was

upcast :: J a -> J (Class "java.lang.Object")

and

unsafeCast :: J a -> J b

However, it would be nice to have some sort of

upcast :: Extends a b => J b -> J a

instance Extends a (J (Class "java.lang.Object"))

instead.

Thanks!

mitchellwrosen avatar Mar 13 '17 20:03 mitchellwrosen

To this end, I've started work on a repo here: https://github.com/mitchellwrosen/java-apache-lucene/blob/master/jvm-extra/src/Language/Java/Extra.hs

However, the interface is inchoate and unstable, as there are many details in the Java type system (inheritance, generics, bounded generics in class/interface definitions e.g. <T extends Number>, wildcard/bounded generics in types, e.g. List<? super Integer>, and on and on, that are quite tricky to model!

I only mention my work here so anyone else interested in tackling this problem can get in touch to compare notes. Or, if there's prior work in this area, maybe I'm wasting my time. Either way, it's fun!

mitchellwrosen avatar Mar 19 '17 17:03 mitchellwrosen

Another possible design is having a class

class Extends (ty1 :: JType) (ty2 :: JType) where

and functions

upcast :: Extends ty1 ty2 => J ty1 -> J ty2
downcast :: Extends ty1 ty2 => J ty2 -> IO (J ty1)
-- Doesn't check at runtime if the input reference has the expected type.
unsafeDowncast :: Extends ty1 ty2 => J ty2 -> J ty1

For a class instance like

class MyClass<A extends BClass & CInterface> extends MySuperClass<A> {
}

We could generate with Java reflection and template haskell:

instance (Extends ty ('Class "BClass"), Extends ty ('Iface "CInterface")) => Extends ('Class "MyClass" <> '[ ty ]) ('Class "MySuperClass" <> '[ ty ])

facundominguez avatar Mar 07 '18 19:03 facundominguez

I think Eta did a good job at this. It's very similar to what you've proposed @facundominguez.

puffnfresh avatar Nov 26 '19 02:11 puffnfresh