Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inheritance #45

Open
mitchellwrosen opened this issue Mar 13, 2017 · 3 comments
Open

Inheritance #45

mitchellwrosen opened this issue Mar 13, 2017 · 3 comments

Comments

@mitchellwrosen
Copy link
Contributor

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
Copy link
Contributor Author

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!

@facundominguez
Copy link
Member

facundominguez commented Mar 7, 2018

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 ])

@puffnfresh
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants