Previously, the implementation of a built-in method looked like:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class EqualRange extends SuMethod2 { | |
{ params = new FunctionSpec(array("value", "block"), Boolean.FALSE); } | |
@Override | |
public Object eval2(Object self, Object a, Object b) { | |
Range r = ((SuContainer) self).equalRange(a, b); | |
return SuContainer.of(r.left, r.right); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Params("value, block=false") | |
public static Object EqualRange(Object self, Object a, Object b) { | |
Range r = ((SuContainer) self).equalRange(a, b); | |
return SuContainer.of(r.left, r.right); | |
} |
To use an annotation, it was easiest to specify the parameter information as a single string description rather than separate arguments. This means a little more work to parse it and split it up, but it makes the description cleaner. Of course, I could have done this before as well.
As before, during startup the methods are picked up by reflection. The difference is that now an instance of a generic class is created, containing a MethodHandle pointing to the static method. I still need the methods and instances to handle adjusting call arguments i.e. filling in default argument values, handling named arguments, and collecting or spreading arguments. (Suneido has a more flexible argument / parameter scheme than Java.)
Once I start using invokedynamic the argument adjustment will probably be done by composing method handles. I could do some of that now if I wanted, but it fits better with invokedynamic.
One advantage of this approach is that the methods can now be static and don't need to take an implicit "this" argument (which I didn't use) in addition to my own explicit "self" argument. Before, they had to be instance methods so I could call them virtually.
Meanwhile, I've been playing with invokedynamic and figuring our how best to use it. I'll post more on this as I progress.
No comments:
Post a Comment