I have 3 light sources classes (X, Y and Z) that inherit from a more general light source class.
2 out of the 3 classes, have a "position" field. (X and Y)
In other method, I run through all light sources of a certain list and I check if the current light source is instance-of Z. If it's not, I would like to access the "position" field of X (or Y) without casting the current instance. But the compiler won't let me, I would like to tell it that on runtime, it would be okay to access this field.
Actually on both cases it won't let me access the field without casting, although on runtime it could not break.
Yeah I could write couple of more lines to handle this, but I ask this out of curiosity and because I like minimalist coding.
for (Light light : lights) {
Vec vector;
if (light instanceof DirLight) vector = light.direction;
else vector = new Vec(intersectionPoint, light.position);
}