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

Subclassing using lambdas #46

Open
lukaseder opened this issue Mar 2, 2018 · 2 comments
Open

Subclassing using lambdas #46

lukaseder opened this issue Mar 2, 2018 · 2 comments

Comments

@lukaseder
Copy link
Member

As originally reported by @C4Phone here: jOOQ/jOOL#331


Hi, I just found out this library and jOOR. I really like your projects. I'd like it better if you guys could achieve "subclassing" interfaces and/or abstract classes using lambdas. This would be very helpful for using Java as an interpreted language.

Expected behavior and actual behavior:

I don't know whether similar functionalities exist. But this is what I expect:

// Run this code in a jupyter notebook.
interface Counter {
  // This is an interface, but it can also be an abstract class
  void add();
  void show();
}

org.joor.Reflect myClass = lambdaSubclass(on(Counter))
  .addField("counter", Integer.class)
  .setDefaultConstructor(self -> self.set("counter", 0))
  .set("add",  self -> self.set("counter", self.get("counter") +1)),
       "show", self -> System.out.println(self.get("counter")))  // "set" is a function of variable length.
  .doneSubclass();

Counter x = myClass.create();
x.add();
x.show();
x.add();
x.show();

// Should print 1 then print 2.

Is this possible or already implemented? If not, can I request this functionality and/or help implement it? Thank you!

@lukaseder
Copy link
Member Author

Thanks for your suggestion. Essentially, you'd like to enhance the existing proxy implementation (which simply delegates getter and setter calls to a HashMap) with the possibility of specifying constructor and method implementations on that proxy?

@markzyu
Copy link

markzyu commented Mar 3, 2018

Yes. But I also worry that using these reflections too much is a bad practice and a reason to use Jython or Scala instead. So maybe people won't use this feature even if we implement it.

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

No branches or pull requests

2 participants