groovy-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Peter Ledbrook <pe...@cacoethes.co.uk>
Subject Comparator argument type inference
Date Mon, 12 Oct 2015 05:51:36 GMT
Hi,

So, I had a difference of opinion with Marcin Erdmann on camera about
closure parameter type inference with functional interfaces. I said it
didn't work in my experience. Thankfully, Marcin worked out why my example:

    import groovy.transform.TypeChecked

    @TypeChecked
    def someMethod() {
        List<Integer> numbers = [1, 4, 56, 35]
        Collections.sort(numbers, { a, b -> a.abs() <=> b.abs() })
    }

doesn't compile. The problem is that Comparator, despite having the
@FunctionalInterface annotation, defines two methods: compare() and
equals(). Interestingly, {...} as Comparator works fine though.

So, does it make sense to add a special case for Comparator so that type
inference works? Or perhaps a special case for any interface that only has
one method that's not defined on Object as well?

Thanks,

Peter

--
Peter Ledbrook
t: @pledbrook
w: http://www.cacoethes.co.uk/

Mime
View raw message