https://kotlinlang.org logo
#datascience
Title
# datascience
k

kyonifer

05/07/2017, 7:00 PM
@danko9 here's one possible workaround i dont hate:
Copy code
// java

// Interface with generic access for unknown primitive type
public interface Matrix<T> {
    void set(Integer i, Integer j, T value);
    T get(Integer i, Integer j);
}

// Adds overrides for specific double intermediate type
// (this would actually be an interface implemented by the backend of choice)
public class DoubleMatrix implements Matrix<Double> {
    @Override
    public void set(Integer i, Integer j, Double value) {
        System.out.println("genericset");
    }
    @Override
    public Double get(Integer i, Integer j) {
        System.out.println("genericget");
        return 0.0;
    }
    
    public void set(int i, int j, double value) {
        System.out.println("primset");
    }
    
    public double get(int i, int j) {
        System.out.println("primget");
        return 0.0;
    }
}

// kotlin

fun main(args: Array<String>) {
    val generic: Matrix<Double> = DoubleMatrix()
    // Erased to Matrix, so uses the generic versions
    generic[1,2]=1.1
    generic[1,2]
    val specific = DoubleMatrix()
    // Kotlin picks the primitive overloads by default
    specific[1,2]=2.2
    specific[1,2]
}