Create instance from Generic

Hello every body.
I want to create an instance from Generic type parameters, but it’s not possible as i have readed, and so i need a new idea for initialize mutablelist with that type like this:

data class Matrix<T>(var elements: MutableList<T> = arrayListOf(), var size: Int=1) {
    init {
        if (elements.isEmpty()) 
            elements = MutableList<T>(count()){
            // init code

It’s not clear what you’re trying to do here. Is the matrix supposed to know how to initialize itself without actually knowing what type its elements are supposed to be? That strikes me as being impossible in any language, not just Kotlin.

I assume your code sample should look more like this:

data class Matrix<T>(var elements: MutableList<T> = mutableListOf(), var size: Int=1) {
    init {
        if (elements.isEmpty())
            elements = MutableList<T>(size) {
                // init code

Note the use of mutableListOf() rather than arrayListOf() as the default value for the elements property, and the use of the size property rather than whatever that count() function is supposed to be.

Assuming that that is correct, what would you like to do in the // init code block?

Thank you for replay mr @mrtact
Before talking about the problem let me explain what i do, i’m making library for scientific computing like numpy in python, but python have a complex numbers built-in, so i have created a class called Complex< T : Number> , and now i want to make matrixes (NOTE: it’s multi dimensional matrix ) operations with all known types Int, Float, Double, Complex, so when i want create an instance from Matrix with setting number of elements only i want to initialize an inner list elements with expected size, so i want to create an instance from T that passed as generic type to initialize the mutableList, and generic types in kotlin is erased at compile time like java, and this is the question How i can create an instance from T at run time?

You can join me to finish the first release quickly

You would need to pass a factory method (or lambda) that creates a instance of T with the desired content.

Very thanks mr @Jonathan.Haas
I’ll try your idea.

Have you tried to see other efforts in the field. There is koma library which mirrors numpy functionalty. I do not think it supports Complex numbers, but still has a lot of things. And there is my own kmath library which does support complex numbers, but has a slightly different paradigm. At this stage it does not make any sense to create multiple mathematical libraries for the same thing. You are welcome to join ongoing discussion at slack.

If you are not registered, use this link.

Thanks @darksnake
Thats great, i did not know it before
so no need to create another library

is koma or kmath supports functions like fft and psd ??

There is a limited JVM support right now via external library (not documented yet). But it will be expanded on demand. You can track it here. You are welcome to create feature requests and contribute.

Thank you mr.@darksnake
I wish to work with you to create the best library
but why you are using external library?

Because developing good mathematical library with high performance takes a lot of time (years) and effort. It makes no sense to reinvent the wheel, when one already has a good solution.The current idea I try to follow in kmath is to have a simple non-optimized multiplatform implementation for light everyday use and platform-specific optimized libraries. I do not use FFT in my work, so for now it has only commons-math implementation in corresponding connector. You are welcome to contribute your own implementation and we can add it to multiplatform part. If for some reason it will be more effective than commons-math, we can use it for performance-critical cases.


I think there is a problem when try to implement this library for kotlin/native and kotlin/js.
i’m trying to implement function from published papers like this:

Cooley, James W., and John W. Tukey, 1965, “An algorithm for the machine calculation of complex Fourier series,”

You can try (probably there are some more recent articles about it). But creating a robust implementation of algorithm from an article is in fact much harder, than it looks. You are welcome to contribute your solution. If you are struggling with the API, you should create a toy implementation first and compare it with commons-maths. I can rewrite it later.

1 Like