« Return to Thread: constructors with mix of params and Map

Re: constructors with mix of params and Map

by Aaron Digulla :: Rate this Message:

Reply to Author | View in Thread

Jochen Theodorou schrieb:

>> That was exactly my point. The initial Groovy example was not under
>> fixed ordering rules, and I have argumented that we should use a fixed
>> order rule if we want to support this feature.
>
> we already have one. named arguments are in a map in the first argument,
> additional positional arguments can be used by a vargs part...
>
> for example
>
> def foo(named=[:], arg1, arg2, Object.. args) {}
>
> well it is working only locally , because Groovy seems to have a little
> problem that I have to fix first.. but anyway, that is how it could be used

Will this work:

args=[a:'x']
foo(args, 1,2)

? I.e. is "args" assigned to "named" in this case?

Also something I'd like to see: Default values for parameters:

def foo(named=[:], arg1, arg2=5, Object... args) {}

Implementation:

Internally, all parameters with default values will be passed via a
varargs part. If there alreay is a varargs part, then they are added at
the front of the varargs array which is automatically created.

If the varargs array has a type != Object, then internally it will be
Object and the original variable arguments are then copied into a new
array with the correct type. Example:

def foo(named=[:], arg1, arg2=5, String... args) {}

will be converted into:

Object foo(Map names, Object arg1, Object... _args) {
    // Handle arguments with default values
    Object arg2; // Use object since no type was specified
    if (_args.length > 0) {
        arg2 = _args[0];
    } else {
        arg2 = new Integer(5); // Maybe extract constant here
                               // (saves a few cycles)
    }

    // Create expected varargs
    int len = Math.min(0, _args.length - 1);
    String[] args = new String[len];
    System.arraycopy (_args, 1, args, 0, len);

    // Original code follows here
}

(from the top of my head but you get the idea).

Regards,

--
Aaron "Optimizer" Digulla a.k.a. Philmann Dark
"It's not the universe that's limited, it's our imagination.
Follow me and I'll show you something beyond the limits."
http://www.pdark.de/

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

 « Return to Thread: constructors with mix of params and Map