3

I have a kotlin math lib with the following Vec2i class and primary constructor:

data class Vec2i(override var x: Int, override var y: Int) : Vec2t<Int>()

Then Vec2i has also, among several secondary constructors, one which is supposed to intercept all the other number types:

constructor(x: Number, y: Number) : this(x.i, y.i)

Everything under Idea compiles and runs. I tested the artifact with a separate java project.

The same artifact doesn't run in a java project if I switch IDE, Netbeans complains about ambiguity between the two.

Why?

Edit: from some further analysis, it turned out Netbeans complains because one of coordinate was int, the other Integer. Trying the same on Idea, it says:

cannot resolve constructor

So, why calling a costructor with (int, int) or (Integer, Integer) is fine, but (int, Integer) doesn't resolved to the secondary constructor and provoke ambiguity?

elect
  • 5,677
  • 9
  • 44
  • 97
  • http://stackoverflow.com/questions/564/what-is-the-difference-between-an-int-and-an-integer-in-java-and-c – hellyale Feb 09 '17 at 15:25
  • Uhm, clear, if you want to answer, I'll accept that – elect Feb 09 '17 at 15:26
  • not sure that is enough for an answer, I just thought it would help. – hellyale Feb 09 '17 at 15:28
  • Well, at begin it seemed to help, but the fact that `(Integer, Integer)` works makes me confused.. I don't know how to explain that – elect Feb 09 '17 at 15:30
  • The constructor has x: Number etc. Integer inherits from Number, so it gets treated as one http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html – hellyale Feb 09 '17 at 15:33
  • @hellyale, btw if you want to answer, I'll accept that – elect Mar 01 '17 at 20:05

1 Answers1

1

At oracles documentation we see that Integer is an object that holds an int.

int is a primitive type, whereas Integer is an object.

The constructors you have allow for passing a pair of either type, but when you pass both the constructor does not know what to do.

For more information this question has a good breakdown.

hellyale
  • 1,551
  • 3
  • 25
  • 49