Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Primitive Auto boxing / unboxing in lambda expression  RSS feed

 
Max Ma
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Having done some tests, I found if the lambda parameter is primitive and it requires the primitive's wrapping class, the compiler will complain, however, if the lambda return a primitive's wrapping class, but actually return a primitive, the compiler doesn't show any issue.

Is it true that the auto boxing is now allowed for lambda parameter?

Please see the question below:

the IntFunction<Integer> requires an int type parameter, return an Integer Object, but we can't pass in Integer Object as parameter whereas return an int type is allowed.
question.PNG
[Thumbnail for question.PNG]
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37234
519
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Max,
You aren't going to like this answer, but "it depends." If the compiler can infer with the boxing, it is allowed. If the boxing makes the lambda ambiguous, the compiler throws up its hands in despair and makes you clarify.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!