[Grace-core] A curiosity of methods with variable arity parameter lists
Andrew P. Black
black at cs.pdx.edu
Tue Aug 6 14:31:28 PDT 2013
While working with mgcollections.set, Jameson and I noticed a curiosity.
If you have a method
withAll(items:Collection<Dynamic>)
and you want to write a method
with(*items:Dynamic)
then it's easy:
method with(*items:Dynamic) { self.withAll(items) }
On the other hand, if you have with(), and want to write withAll(), then there is no way to do it without using reflection. Even then, the code is messy; this was what I came up with:
method withAll(items:Collection<Dynamic>) {
def selfMirror = mirror.reflect(self)
def argLists = list.with(items)
def withMethod = selfMirror.forMethod "with"
withMethod.request(argLists)
}
The moral of the story, I think, is that we should make it a practice in defining library interfaces never to define a variable arity method directly, but instead to always define a method that takes an array of arguments, and then to define the varArity method in terms of that (as I did above for method "with"). Notice that the bodies of withAll and with are identical: in both cases, items is a Collection.
Andrew
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailhost.cecs.pdx.edu/pipermail/grace-core/attachments/20130806/9184c344/attachment.html>
More information about the Grace-core
mailing list