Groovy - adding methods similar to JS prototype?
up vote
1
down vote
favorite
With the future of JS on Java unclear (JS/Nashorn deprecated on Java 11), I've been looking into options completely outside of JS, Groovy being a major one given its popularity and similarity to Java.
I currently use JS/Nasahorn to handle a lot of actions for a game server, and while a lot of it I can see can be converted to Groovy, I do have one concern that I'm having trouble figuring out if possible or not.
So with my game server, there are new clickable map objects and npcs being added all the time (with a total of each being in the tens of thousands). To deal with the click actions of these, I have JS classes to handle them.
To add a new object or npc action, I have groups of JS files that split the ids up so that it isn't one giant file. Specifically, I use the JS prototype to be able to add these actions in without being the same file, and just load all the JS files at runtime.
ObjectOption.prototype.object_0 = function(player, object) { }
And then I'm able to execute the functions from the actual JS class itself via
ObjectOption.prototype["object_" + object.getID()](player, object);
or
this["object_" + object.getID()](player, object);
My question is, can any such setup be done with Groovy, or is this outside the scope of how Groovy works?
groovy
add a comment |
up vote
1
down vote
favorite
With the future of JS on Java unclear (JS/Nashorn deprecated on Java 11), I've been looking into options completely outside of JS, Groovy being a major one given its popularity and similarity to Java.
I currently use JS/Nasahorn to handle a lot of actions for a game server, and while a lot of it I can see can be converted to Groovy, I do have one concern that I'm having trouble figuring out if possible or not.
So with my game server, there are new clickable map objects and npcs being added all the time (with a total of each being in the tens of thousands). To deal with the click actions of these, I have JS classes to handle them.
To add a new object or npc action, I have groups of JS files that split the ids up so that it isn't one giant file. Specifically, I use the JS prototype to be able to add these actions in without being the same file, and just load all the JS files at runtime.
ObjectOption.prototype.object_0 = function(player, object) { }
And then I'm able to execute the functions from the actual JS class itself via
ObjectOption.prototype["object_" + object.getID()](player, object);
or
this["object_" + object.getID()](player, object);
My question is, can any such setup be done with Groovy, or is this outside the scope of how Groovy works?
groovy
Yes, you can, in a variety of ways (see the metaprogramming docs), but I don't see a need to do it like this, at least not yet. Why can't you just instantiate whatever sort of handler this is and add it to a hash keyed by ID when you need them? They're on the classpath and and can be instantiated at will. Perhaps I'm not really understanding the usecase here yet.
– Dave Newton
Nov 13 at 22:18
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
With the future of JS on Java unclear (JS/Nashorn deprecated on Java 11), I've been looking into options completely outside of JS, Groovy being a major one given its popularity and similarity to Java.
I currently use JS/Nasahorn to handle a lot of actions for a game server, and while a lot of it I can see can be converted to Groovy, I do have one concern that I'm having trouble figuring out if possible or not.
So with my game server, there are new clickable map objects and npcs being added all the time (with a total of each being in the tens of thousands). To deal with the click actions of these, I have JS classes to handle them.
To add a new object or npc action, I have groups of JS files that split the ids up so that it isn't one giant file. Specifically, I use the JS prototype to be able to add these actions in without being the same file, and just load all the JS files at runtime.
ObjectOption.prototype.object_0 = function(player, object) { }
And then I'm able to execute the functions from the actual JS class itself via
ObjectOption.prototype["object_" + object.getID()](player, object);
or
this["object_" + object.getID()](player, object);
My question is, can any such setup be done with Groovy, or is this outside the scope of how Groovy works?
groovy
With the future of JS on Java unclear (JS/Nashorn deprecated on Java 11), I've been looking into options completely outside of JS, Groovy being a major one given its popularity and similarity to Java.
I currently use JS/Nasahorn to handle a lot of actions for a game server, and while a lot of it I can see can be converted to Groovy, I do have one concern that I'm having trouble figuring out if possible or not.
So with my game server, there are new clickable map objects and npcs being added all the time (with a total of each being in the tens of thousands). To deal with the click actions of these, I have JS classes to handle them.
To add a new object or npc action, I have groups of JS files that split the ids up so that it isn't one giant file. Specifically, I use the JS prototype to be able to add these actions in without being the same file, and just load all the JS files at runtime.
ObjectOption.prototype.object_0 = function(player, object) { }
And then I'm able to execute the functions from the actual JS class itself via
ObjectOption.prototype["object_" + object.getID()](player, object);
or
this["object_" + object.getID()](player, object);
My question is, can any such setup be done with Groovy, or is this outside the scope of how Groovy works?
groovy
groovy
asked Nov 13 at 22:09
PalidinoDH
65
65
Yes, you can, in a variety of ways (see the metaprogramming docs), but I don't see a need to do it like this, at least not yet. Why can't you just instantiate whatever sort of handler this is and add it to a hash keyed by ID when you need them? They're on the classpath and and can be instantiated at will. Perhaps I'm not really understanding the usecase here yet.
– Dave Newton
Nov 13 at 22:18
add a comment |
Yes, you can, in a variety of ways (see the metaprogramming docs), but I don't see a need to do it like this, at least not yet. Why can't you just instantiate whatever sort of handler this is and add it to a hash keyed by ID when you need them? They're on the classpath and and can be instantiated at will. Perhaps I'm not really understanding the usecase here yet.
– Dave Newton
Nov 13 at 22:18
Yes, you can, in a variety of ways (see the metaprogramming docs), but I don't see a need to do it like this, at least not yet. Why can't you just instantiate whatever sort of handler this is and add it to a hash keyed by ID when you need them? They're on the classpath and and can be instantiated at will. Perhaps I'm not really understanding the usecase here yet.
– Dave Newton
Nov 13 at 22:18
Yes, you can, in a variety of ways (see the metaprogramming docs), but I don't see a need to do it like this, at least not yet. Why can't you just instantiate whatever sort of handler this is and add it to a hash keyed by ID when you need them? They're on the classpath and and can be instantiated at will. Perhaps I'm not really understanding the usecase here yet.
– Dave Newton
Nov 13 at 22:18
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
Groovy supports metaprogramming that allows you e.g. adding new methods to existing classes at the runtime. Consider following example:
class Person {
void greetings(String name) {
println "Greetings, $name!"
}
}
Person.metaClass.hello = { String name -> println "Hello, $name!" }
def john = new Person()
john.greetings("Paul")
john.hello("Mary")
Output:
Greetings, Paul!
Hello, Mary!
In this example, we have a simple Person
class with a single method greetings()
. We are able to add a new method hello()
by accessing Person.metaClass
object and adding method hello
as a closure that accepts a single parameter name
of type String
.
Check the Groovy's documentation on metaprogramming, you will find many examples there.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Groovy supports metaprogramming that allows you e.g. adding new methods to existing classes at the runtime. Consider following example:
class Person {
void greetings(String name) {
println "Greetings, $name!"
}
}
Person.metaClass.hello = { String name -> println "Hello, $name!" }
def john = new Person()
john.greetings("Paul")
john.hello("Mary")
Output:
Greetings, Paul!
Hello, Mary!
In this example, we have a simple Person
class with a single method greetings()
. We are able to add a new method hello()
by accessing Person.metaClass
object and adding method hello
as a closure that accepts a single parameter name
of type String
.
Check the Groovy's documentation on metaprogramming, you will find many examples there.
add a comment |
up vote
1
down vote
Groovy supports metaprogramming that allows you e.g. adding new methods to existing classes at the runtime. Consider following example:
class Person {
void greetings(String name) {
println "Greetings, $name!"
}
}
Person.metaClass.hello = { String name -> println "Hello, $name!" }
def john = new Person()
john.greetings("Paul")
john.hello("Mary")
Output:
Greetings, Paul!
Hello, Mary!
In this example, we have a simple Person
class with a single method greetings()
. We are able to add a new method hello()
by accessing Person.metaClass
object and adding method hello
as a closure that accepts a single parameter name
of type String
.
Check the Groovy's documentation on metaprogramming, you will find many examples there.
add a comment |
up vote
1
down vote
up vote
1
down vote
Groovy supports metaprogramming that allows you e.g. adding new methods to existing classes at the runtime. Consider following example:
class Person {
void greetings(String name) {
println "Greetings, $name!"
}
}
Person.metaClass.hello = { String name -> println "Hello, $name!" }
def john = new Person()
john.greetings("Paul")
john.hello("Mary")
Output:
Greetings, Paul!
Hello, Mary!
In this example, we have a simple Person
class with a single method greetings()
. We are able to add a new method hello()
by accessing Person.metaClass
object and adding method hello
as a closure that accepts a single parameter name
of type String
.
Check the Groovy's documentation on metaprogramming, you will find many examples there.
Groovy supports metaprogramming that allows you e.g. adding new methods to existing classes at the runtime. Consider following example:
class Person {
void greetings(String name) {
println "Greetings, $name!"
}
}
Person.metaClass.hello = { String name -> println "Hello, $name!" }
def john = new Person()
john.greetings("Paul")
john.hello("Mary")
Output:
Greetings, Paul!
Hello, Mary!
In this example, we have a simple Person
class with a single method greetings()
. We are able to add a new method hello()
by accessing Person.metaClass
object and adding method hello
as a closure that accepts a single parameter name
of type String
.
Check the Groovy's documentation on metaprogramming, you will find many examples there.
answered Nov 14 at 8:20
Szymon Stepniak
16.3k83062
16.3k83062
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290252%2fgroovy-adding-methods-similar-to-js-prototype%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Yes, you can, in a variety of ways (see the metaprogramming docs), but I don't see a need to do it like this, at least not yet. Why can't you just instantiate whatever sort of handler this is and add it to a hash keyed by ID when you need them? They're on the classpath and and can be instantiated at will. Perhaps I'm not really understanding the usecase here yet.
– Dave Newton
Nov 13 at 22:18