Can I add new features into Java like delegate, late binding, etc?

107 views Asked by At

Is there any way that I can add delegate features into Java by a library for example I write add library that add delegate as keyword and handle delegate features like C#

delegate void SendMessageDelegate(Message message);

void SendMessage(SendMessageDelegate sendMessageDelegateReference)
{
  sendMessageDelegateReference(new Message("hello this is a sample message"));
}
void HandleSendMessage(Message message)
{
  Sender.Send(message);
} 

In general, my question is: is there any way to add my new features such as:

  • late binding
  • delegates
  • events like C#
  • properties

by one or more libraries into Java instead of create a new compiler such as groovy or scala?

2

There are 2 answers

7
Tim B On BEST ANSWER

The simple answer is no. Not with any reasonable amount of work.

However you can probably implement some or all of those features using the existing Java tools (or find a library that already does it).

You wouldn't be able to add a new keyword to the language - but it's amazing what you can do with creative use of annotations, generics, etc.

Google found this: https://today.java.net/pub/a/today/2004/10/06/compiler.html

I've no idea how good it is.

0
user2684301 On

No, you can't just add your own keywords without writing a custom compiler.

JDK8, which is pretty usable but still in pre-reelase, has labmdas which covers delegates/events pretty well. Pre-JDK8 has a very bulky syntax for lambdas (anonymous inner classes) and JDK7 has MethodHandles which are similar to Delegates.

Properties? Scala has a much better solution to that issue than C# that doesn't require separate property/field constructs.