Issue
I have this function in side an interface
interface test<Int>{
fun printInt():Int {
return 2}}
The error is: the integer literal doesn't conform to the expected type Int.
- If I change the return type to
kotlin.Int, the error goes away.
interface test<Int>{
fun printInt(): **kotlin.Int** {
return 2}}
- I don't use return, it works fine like this:
interface test<Int>{
fun printInt() = 2
}
- if I get the printInt function out of the interface, the compiler doesn't complain:
fun printInt(): **Int** {
return 2}
what are these Int#1 and kotlin.Int?
Solution
This declaration:
interface test<Int>
declares a generic interface, with a type parameter called Int. This is similar to how MutableList is a generic interface with a type parameter called E:
public interface MutableList<E>
Therefore, inside the interface, the unqualified name "Int" refers to the type parameter, (similar to how inside MutableList, E refers to the type parameter) not the kotlin.Int type.
Answered By - Sweeper
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.