I’m trying to do something similar to KeepLast in Scala, but I don’t seem to be able to figure out a way to do generic types. I don’t know where to even begin. I’ve been trying to teach myself Scala now for 3 days and have been googling constantly trying to figure this out.
/**
-
Class I’m trying to figure out how to do in Scala
-
@author Michael
-
@param
*/
public class KeepLast {
private T value;
private T lastValue;public KeepLast(T value) {
this.value = value;
}public void setValue(T newValue) {
lastValue = value;
value = newValue;
}public T getValue() { return value; }
public T getLastValue() { return lastValue; }
}
/**
- Simple example of use
- @author Michael
*/
public class MultipleTypesTest {
private KeepLast<Integer> keepInteger;
private KeepLast<String> keepString;
private KeepLast<Boolean> keepBool;
public MultipleTypesTest() {
keepInteger = new KeepLast<Integer>(0);
keepString = new KeepLast<String>("not initialized");
keepBool = new KeepLast<Boolean>(false);
}
public KeepLast<Integer> getKeepInteger() { return keepInteger; }
public KeepLast<String> getKeepString() { return keepString; }
public KeepLast<Boolean> getKeepBoolean() { return keepBool; }
public static void main(String[] args) {
MultipleTypesTest testOb = new MultipleTypesTest();
System.out.println("" + testOb.getKeepInteger().getValue());
System.out.println("" + testOb.getKeepString().getValue());
System.out.println("" + testOb.getKeepBoolean().getValue());
System.out.println();
testOb.getKeepInteger().setValue(17);
testOb.getKeepString().setValue("Hello World");
testOb.getKeepBoolean().setValue(true);
System.out.println("" + testOb.getKeepInteger().getValue());
System.out.println("" + testOb.getKeepString().getValue());
System.out.println("" + testOb.getKeepBoolean().getValue());
}
}