Variance with js.Array

105 Views Asked by At

I have a javascript method that accepts js.Arrays that can contain objects of multiple types. E.g js.Dates or Integers or Strings

How I can model that without having to cast any of these types.

def domain(p: js.Array[js.Any])

does not work with

domain(js.Array(new js.Date(2015,1,1))

as js.Array is invariant.

def domain[T <: js.Any](p: js.Array[T])

does not work with

domain(js.Array("Test")) or domain(js.Array(0,2))

as String and Int do not inherit from js.Any. I have seen that there is an implicit conversion from Int to js.Any but that does not seem to kick in

 inferred type arguments [Int] do not conform to method domain's type
 parameter bounds [T <: scala.scalajs.js.Any]

I am a bit puzzled anyway. Intellij does not show me an error while fastOptJS from sbt throws a compilation error.

1

There are 1 best solutions below

0
On BEST ANSWER

Found the solution. Upper View Bounds:

def domain[T <% js.Any](p: js.Array[T])