Shadowlands
Shadowlands

Reputation: 15074

How to convert an array of AnyRef to a tuple of Ints

I needed to convert an array of AnyRef (java function argument) to a tuple of Ints. I tried the accepted answer from here, but this didn't work for me because in that question it was assumed that the array was an array of Any, not an array of AnyRef.

Upvotes: 0

Views: 1642

Answers (1)

Shadowlands
Shadowlands

Reputation: 15074

In the end, the code that worked for me looked as follows:

val (x, y) = args match { case Array(x: Integer, y: Integer, _*) => (x, y) }

Note the use of the boxing class Integer rather than Int to deal with the fact that we are sourcing from auto-boxed AnyRefs (java Objects).

Upvotes: 3

Related Questions