How to work with generic type array and generic type class as parameter in constructor?

Abir Chokraborty

I want to convert the following simple java code into scala code. I know T <: Comparable[T] will be used for T extends Comparable. For generic type array, I have to use either Manifest or ClassManifest but I could not convert the two constructors for scala code.

public class MyClass<T extends Comparable> {

    private static int MAX_SIZE = 40;
    private T[] array;
    private int count = 0;

    public MyClass(Class<T> clazz) {
        this(clazz, MAX_SIZE);

    public MyClass(Class<T> clazz, int size) {
        array = (T[]) Array.newInstance(clazz, size);
Yuval Itzchakov

This is a rough sketch of what a Scala class would look like:

import scala.reflect.ClassTag

class MyClass[T : Ordered : ClassTag](size: Int) {
  val arr: Array[T] = new Array[T](size)

  def this() {

Because of Scala auxiliary constructor initialization order, you can't access this inside the constructor (that is the reason I hard coded 40 instead of settings a max value field). If you want to get around that, you can define a companion object to MyClass with an apply method which takes no argument:

import scala.reflect.ClassTag

class MyClass[T : Ordered : ClassTag](size: Int) {
  val arr: Array[T] = new Array[T](size)

object MyClass {
  final val maxSize: Int = 40
  def apply[T : Ordered : ClassTag]() = new MyClass[T](maxSize)

And then utilize it like this:

val clz = MyClass[Int]()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at


Login to comment


Using generic class as type parameter

Get type name for generic parameter of generic class

How to get class of generic type when there is no parameter of it?

class parameter of generic type

Create array of generic type of class

Infer generic class type parameters from single constructor parameter

Get array class of generic type

"Expected type parameter" error in the constructor of a generic struct

Type Parameter Class with generic java

How to use generic parameter type for another generic class?

How to check generic class type is array?

Distinguish between a class generic type parameter and method generic type parameter

Generic inner class constructor accepts different type parameter what is set?

How to get class of generic type parameter in Kotlin

how to pass generic list of unknown type to class constructor

How to define a parameter of type generic list with constructor constraint?

generic type of child class constructor

Use generic type in class constructor

How to convert a string array to a generic array type while allowing both non array and array as generic type parameter for the class?

How to pass a generic type object to a class constructor

How to return class type of a generic class with generic?

Generic type as a constructor parameter

Generic class as parameter with inner type

How to pass a type parameter to a generic class constructor reference?

How to register/resolve a type with one generic constructor parameter

How to add a primary constructor to a generic class with type constraints?

How to create a generic class with generic using a known type as parameter?

Call constructor of type parameter in generic class

How to use constructor of Generic type

TOP Ranking

  1. 1

    How can I create a column whose entries depend on the contents of a column in another table?

  2. 2 port in URL

  3. 3

    How to display items using sessions

  4. 4

    BigQuery - concatenate ignoring NULL

  5. 5

    no applicable method for 'mutate_' applied to an object of class "c('integer', 'numeric')"

  6. 6

    How i extract text from a model dialog in selenium?

  7. 7

    x no applicable method for 'tbl_vars' applied to an object of class "c('double', 'numeric')"

  8. 8

    AirflowException: Celery command failed - The recorded hostname does not match this instance's hostname

  9. 9

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  10. 10

    Most efficient regex for checking if a string contains at least 3 alphanumeric characters

  11. 11

    using excel range find return Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))

  12. 12

    redirect your computer port to url

  13. 13

    Can't create database with Symfony 4.1 : could not find driver

  14. 14

    How to fix "Exception evaluating SpringEL expression" error after submitting a variable Spring/Thymeleaf

  15. 15

    A python function to get the maximum value of a specific column, given a string that represents a table in CSV format

  16. 16

    Double spacing in rmarkdown pdf

  17. 17

    Uploading Files on production server returns either CORS error or POST 400 Bad Request using Apollo-Graphql

  18. 18

    Android Kotlin Unit test failing with io.mockk.MockKException: no answer found for

  19. 19

    Change Model input_shape but got an : ValueError: Input 0 of layer dense_44 is incompatible with the layer

  20. 20

    Formik, Yup - How to check is Decimal number

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class