[Solved] Type mismatch: inferred type is List<main.Number> but List<kotlin.Number> was expected

I do not understand what is the difference between main.Number and kotlin.Number I am create a class in the main function in my program named Number and tried to put like constructor n1 a number that the user input when I try to save this a list the type Number type mismatch is throw . How can I fix this


You’re declaring the list before the number, and so kotlin is assuming that you mean the built-in Number class, instead of your own local Number class. If you move the list declaration to be above the list, then everything should compile successfully.

1 Like

this work, thaks. But why i try printthis number pops up

That’s because Number is declared as a normal class, and so its toString() method is the default one, which just prints its name and an identifier. Instead, declare it as data class Number(...), which automatically adds the toString() function sensibly. I’d also suggest you read some of the introduction docs

1 Like

where I click for say this topic was solved ?

There isn’t an option here for this unlike StackOverflow. I’d say just edit your post to say “[Solved]” at the start of it