Re: Variable Type-Checking
Not at compile time. You can use reflection to type-check at run time. However, I would strongly recommend refactoring your code if you're resorting to doing that.
edit: haha, got confused on the terminology. It's reflection, not refraction.
Re: Variable Type-Checking
I got the "reflection" part, ;).
Truth is, I'm actually refactoring code right now because the way we've implemented it (using J2SE) is we use method overloading to implement it. Basically, we use:
function1(FieldType1, ValueType1);
function2(FieldType2, ValueType2);
...
functionN(FieldTypeN, ValueTypeN);
which implements the same functionality but are able to use Java's method signature resolution at compile-time. I was hoping it could be made generic.
Re: Variable Type-Checking
Hmm, if you have the same functionality regardless of the type, you can either use generic parameterization or polymorphism. Without knowing more about what it is you want to do I can't determine which one to use (both should work in theory). I tend to lean towards polymorphism because it's more clean and concise, but could require more code refactoring than is necessary (especially if the original code was never designed to take advantage of inheritance/polymorphism).
Re: Variable Type-Checking
May or may not suit this scenario, but you could extend one of the types from the other
Code :
public class MyClass<V, T extends V>{
///my class
public void function(V v, T t){
}
}
If necessary you could wrap any values into another object/interface to force compile time checking, but again I'm not fully sure this will suit your scenario (as well as being simply overkill)
Re: Variable Type-Checking
I tried the following snippet and it's not working as I was hoping it would, I'm coding out of ignorance right now:
Code :
package blah;
import java.util.Hashtable;
import java.util.Map;
public class TestModel {
private Map<Object, Object> container = new Hashtable<Object, Object>();
public static void main(String[] args) {
TestModel v = new TestModel();
v.add("hello", new Integer(2)); // There should be a compile-time error here
v.add("hello", "there"); // But not here
System.out.println("Value = " + v.getContainer().get("hello"));
}
public<K> void add(K fieldType, K value) {
container.put(fieldType, value);
}
public void setContainer(Map<Object, Object> container) {
this.container = container;
}
public Map<Object, Object> getContainer() {
return container;
}
}
Re: Variable Type-Checking
a) Define your class to be Generic
Code :
public class TestModel<K>{
b) no need for the generic near the public
Code :
public void add(K fieldType, K value)//remove the 'public<K>'
Re: Variable Type-Checking
Quote:
Originally Posted by
copeg
a) Define your class to be Generic
Code :
public class TestModel<K>{
b) no need for the generic near the public
Code :
public void add(K fieldType, K value)//remove the 'public<K>'
But this is tying down the class type for value at the time the model variable is defined. I was hoping to tie it down to the fieldType parameter at compile-time. Note the two usage examples I had and the expected results:
Code :
v.add("hello", new Integer(2)); // There should be a compile-time error here
v.add("hello", "there"); // But not here