0

I am curious as to how to add an ArrayBuffer element via a def. E.g.

def addToArray[T](data: ArrayBuffer[T]): ArrayBuffer[T] = {
      return(data += T("XYZ"))
}

I tried this but no go. I assume we cannot do this generically, but I would like to know how to do this. If I do this, return(new ArrayBuffer[T]()), it works. Not the most difficult but somehow escaping me.

10
  • What di you expected T("XYZ") to do? Commented Aug 27, 2022 at 16:02
  • @LuisMiguelMejíaSuárez Hi, it's just a contrived example to add an element to existing ArrayBuffer[T] represented by data. e.g. 3 items of class T, then 4 items. Seems fine inline, but via defs not so easy. Commented Aug 27, 2022 at 16:04
  • @LuisMiguelMejíaSuárez I cannot really find a suitable reference for this type of stuff. Commented Aug 27, 2022 at 16:05
  • If you want to use an specific type T then why use a type parameter of the same name? Commented Aug 27, 2022 at 16:07
  • @LuisMiguelMejíaSuárez you will have to elaborate pls Commented Aug 27, 2022 at 16:09

1 Answer 1

2

Just give the def your generic buffer and the element you want to add then return it:

  def addToArrayBuffer[T](data: ArrayBuffer[T], elem: T): ArrayBuffer[T] = {
    data += elem
    data
  }
  println(addToArrayBuffer(ArrayBuffer(1, 2, 30), 4)) // ArrayBuffer(1, 2, 30, 4)

If you are not passing the element you want to add as a parameter to the def, then you can't add it inside the def. The idea is that you cannot create an instance of a type parameter, because instantiation requires a constructor which is unavailable if the type is unknown. This restriction is mentioned in the Java generics spec here:

Cannot Create Instances of Type Parameters: You cannot create an instance of a type parameter. For example, the following code causes a compile-time error:

public static <E> void append(List<E> list) {
    E elem = new E();  // compile-time error
    list.add(elem);
}

Wikipedia also explains this very nicely:

Java generics differ from C++ templates. Java generics generate only one compiled version of a generic class or function regardless of the number of parameterizing types used. Furthermore, the Java run-time environment does not need to know which parameterized type is used because the type information is validated at compile-time and is not included in the compiled code. Consequently, instantiating a Java class of a parameterized type is impossible because instantiation requires a call to a constructor, which is unavailable if the type is unknown.

Note that there might be a workaround to this using reflection, which is further detailed in the spec.

Sign up to request clarification or add additional context in comments.

3 Comments

OK that is actually what I was working towards, but wondering why data += T("ZZZZZZ") does not work? Instead of data += elem
@thebluephantom because T("ZZZZZZ") doesn't make any sense in the context of your code, which is why I asked what you wanted it to do. - Now, note that the method in this answer is basically useless since it is even longer than just calling += when you need it, and is not abstracting anything.
I updated my answer to address you concern. The idea is you can't instantiate an unknown type, because you require a constructor, which is unavailable if the type is unknown. So T("ZZZZZZ") does not compile. Luis is right.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.