0
votes

i have a function which returns generics:

def getArray(tag: Tags, arr: Option[Array[SearchHit]]
): Array[_ >: CustomerInfoDTO[CustomerApplicationIdDTO] with CustomerIdDTO <: Serializable] = arr match {
    case Some(s) =>
      tag match {
        case GetCustomersInfo =>
          s.map(x => extractCustomerInfo(x.sourceAsString))
        case GetCustomersId =>
          s.map(x => extractCustomerId(x.sourceAsString))
        case _ => throw new NoSuchElementException("Can't match tag")
      }
    case None => throw new NoSuchElementException("Empty Array")
  }

so, my problem when i'm trying to match a fuction result:

case arr: Array[CustomerInfoDTO[CustomerApplicationIdDTO]] =>

i'm getting a warning "non-variable type argument CustomerApplicationIdDTO in type pattern Array[CustomerInfoDTO[CustomerApplicationIdDTO]] is unchecked since it is eliminated by erasure"

Is it means, that in Array[] it possible to get array of any type? So i have readed about ClassTag and TypeTag, but misunderstood how to use it in my case. So can you help me? how to handle this warning?

1

1 Answers

0
votes

Note that it's complaining about CustomerApplicationIdDTO, not CustomerInfoDTO: Array is unique in that you can actually test for Array[CustomerInfoDTO[_]], so case arr: Array[CustomerInfoDTO[_]] => won't give a warning.

But even if you intended to say "this function returns either an Array[CustomerInfoDTO[CustomerApplicationIdDTO]] or an Array[CustomerIdDTO]", that's not what it does; their supertypes (up to Serializable can also be there, in particular this function is allowed to return an Array[CustomerInfoDTO[_]]. Since pattern matching can't distinguish between Array[CustomerInfoDTO[_]] and Array[CustomerInfoDTO[CustomerApplicationIdDTO]], you get a warning.

You should think what benefit do you actually get from this being a single function instead of two getCustomerInfoArray and getCustomerIdArray which would be simpler to use.

But if you decide it is what you want, your options are:

  1. Handle just case arr: Array[CustomerInfoDTO[_]] =>.

  2. If you are certain you won't get Array[CustomerInfoDTO[AnythingElse]] from this function, you can tell that to the compiler using case arr: Array[CustomerInfoDTO[CustomerApplicationIdDTO]] @unchecked => (I think; maybe you need Array[CustomerInfoDTO[CustomerApplicationIdDTO @unchecked]]).

  3. use TypeTags or ClassTags, as you mention, but this is going to complicate things even more.