What would the equivalent to Java 8 :: (double colon operator) in Groovy?
I'm trying to translate this example in groovy https://github.com/bytefish/PgBulkInsert
But the mapping part doesn't work the same way as Java 8:
public PersonBulkInserter() {
super("sample", "unit_test");
mapString("first_name", Person::getFirstName);
mapString("last_name", Person::getLastName);
mapDate("birth_date", Person::getBirthDate);
}
Groovy doesn't really have instance-divorced instance-method references (EDIT: Yet. See Wavyx's comment on this answer.), so instead you have to fake it with closures. When using instance-method reference syntax in Java 8, you are really setting up the equivalent of a lambda that expects the invoking instance as its first (in this case, only) argument.
Thus, to get the same effect in Groovy we have to create a closure that uses the default
it
argument as the invoking instance. Like this:Notice the use of Groovy property notation here, and that it is necessary to cast the
Closure
to the@FunctionalInterface
type expected by themapString()
ormapDate()
method.