What is the difference between `’a.` and `type a.` and when to use each?

Below are alternative explanations with a varying amount of detail, depending on how much of a hurry you’re in. 😉 I will use the following code (drawn from that other question) as a running example. Here, the type annotation on the definition of reduce is actually required to make it typecheck. (* The type [(‘a, … Read more

Multiple parameter closure argument type not inferred

See this scala-debate thread for a discussion of what’s going on here. The problem is that Scala’s type inference happens per parameter list, not per parameter. As Josh Suereth notes in that thread, there’s a good reason for the current approach. If Scala had per-parameter type inference, the compiler couldn’t infer an upper bound across … Read more

Cannot use Java 8 method with lambda arguments without specifying type arguments

Holger had the best answer in the comment section in my opinion: This is a known limitation of Java 8’s type inference: it doesn’t work with chained method invocations like genericFactoryMethod().build(). Thanks! About my API, I will specify the functions before using them as arguments, like this: Function<Entry<String, Set<String>>, String> keyMapper = Entry::getKey; Function<Entry<String, Set<String>>, … Read more

Collections.emptyList() returns a List?

The issue you’re encountering is that even though the method emptyList() returns List<T>, you haven’t provided it with the type, so it defaults to returning List<Object>. You can supply the type parameter, and have your code behave as expected, like this: public Person(String name) { this(name,Collections.<String>emptyList()); } Now when you’re doing straight assignment, the compiler … Read more

Iterable cannot confirm generic T in function

Yuck, yeah, I see that the default inference doesn’t work deeply enough to unroll Iterable<Iterable<T>> into T. It’s not that surprising if you look at how the typings for Iterable are defined in the relevant library: interface Iterable<T> { [Symbol.iterator](): Iterator<T>; } An Iterable<T> has a symbol-keyed method whose return type is Iterator<T>, which itself … Read more