0

Currently, I am working on a project to transpile from my company's in house scripting language, which is Object Orientated and takes quite a few features from other languages, into Groovy, which has many similar features.

To keep code as close to original as possible, I am trying to leave certain function names and parameters the same. To cater for this, I would like to write a set of libraries that can be imported.

For example, say I have an inbuilt method in the original scripting language, I would like to be able to write the definition for this method in a groovy file, that can then be imported when needed, and the method may be called.

Tools.groovy

// filename: Tools.groovy
public String foo(String bar) {
    return bar;
}

and in another file

Main.groovy

// filename: Main.groovy
import Tools;

String bat = foo("bar")

I know you can can compile class files into jars and put them into the class path, but a lot of the methods I will need to implement will either require meta programming or won't be associated with an object.

Sorry if it's either a bad question or not clear enough. I'm not sure whether its even possible.

Cheers

rhysvo
  • 38
  • 6

1 Answers1

1

I believe you should be able to create libraries and reuse them when needed.

All you need to do is create class and add the static methods if you do not have to create instances, non static methods otherwise. Then it looks like you already aware how to proceed later.

For instance, you can create utilities classes for String, List, etc based on your description.

By the way, even if you do not create libraries, it is even possible to write one lines in groovy achieve what you may needed most of the cases.

Rao
  • 20,781
  • 11
  • 57
  • 77
  • I was trying to avoid having to use the class notation with static methods, such as Foo.bar(), but it looks like that is going to be the best option. As for creating utility methods in libraries for String and List etc, with out creating a sub class, the best way would be vie meta programming. Is it possible to create a library of say String.metaClass.* methods that can be imported? Cheers – rhysvo Apr 12 '17 at 22:34