5

Interesting compilation error in Lambda Java 8 (Oracle JDK)

java -version
java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

I have a method call:

new CSVFile()
.of(new FileInputStream("MyFile.csv"))
.withColumnMapping("name", "fullName", s -> s.toUpperCase())
.withColumnMapping("gender", "gender", s -> s.toUpperCase());

This is the method I'm trying to call:

 public CSVFile withColumnMapping(final String columnName, final String   beanPropertyName, final Function<String, Object> columnTransformFunction) {
    columnMappings.add(new ColumnMapping(columnName, beanPropertyName, Optional.of(columnTransformFunction)));
    return this;
}

Compilation Error I get is:

[ERROR] /Users/sai/fun/reactivecsv/src/test/java/reactivecsv/CSVFileTest.java:[26,50] cannot find symbol
[ERROR] symbol:   method toUpperCase()
[ERROR] location: variable s of type java.lang.Object

Weirdly, This compiles

Function<String, Object> upperCaseConversion = String::toUpperCase;
new CSVFile()
.of(new FileInputStream("MyFile.csv"))
.withColumnMapping("name", "fullName", upperCaseConversion)
.withColumnMapping("gender", "gender", upperCaseConversion);

Why is the compiler not able to synthesise the lambda into a Function ?

3
  • 1
    By any change how your CSVFile class is declared? Is it generic? Commented Jul 29, 2015 at 10:53
  • That is correct: public class CSVFile<T> Commented Jul 29, 2015 at 10:54
  • 3
    Hence new CSVFile() is a raw type. Commented Jul 29, 2015 at 10:56

1 Answer 1

8

When you create new CSVFile() for generic type, it becomes a raw type. You must not use raw types. Creating it as raw type changes all its methods to raw as well, thus withColumnMapping(String, String, Function<String, Object>) becomes withColumnMapping(String, String, Function) and type of your lambda argument cannot be inferred. To fix your problem specify the proper generic arguments when calling the constructor.

Sign up to request clarification or add additional context in comments.

1 Comment

That's indeed the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.