I have this class:
public class Transform<PositionType, RotationType, ScaleType>
where PositionType : Position
where RotationType : Rotation
where ScaleType : Scale
{
public Transform<PositionType, RotationType, ScaleType> Parent;
public PositionType GlobalPosition;
// The next line has a compile error: Cannot implicitly convert type 'Position'
// to 'PositionType'. An explicit conversion exists (are you missing a cast?)
public PositionType LocalPosition => Parent.GlobalPosition - GlobalPosition;
public RotationType GlobalRotation;
// The next line has a compile error: Cannot implicitly convert type 'Rotation'
// to 'RotationType'. An explicit conversion exists (are you missing a cast?)
public RotationType LocalRotation => Parent.GlobalRotation - GlobalRotation;
public ScaleType GlobalScale;
// The next line has a compile error: Cannot implicitly convert type 'Scale'
// to 'ScaleType'. An explicit conversion exists (are you missing a cast?)
public ScaleType LocalScale => Parent.GlobalScale - GlobalScale;
}
Position: (Scale and Rotation are defined in the same way)
public class Position
{
public Position(int axis)
{
Axis = new float[axis];
}
public Position(float[] axis)
{
Axis = axis;
}
public static Position operator -(Position a, Position b)
{
if (a.Axis.Length != b.Axis.Length)
{
throw new System.Exception("The axis of the two Positions are not comparable.");
}
Position difference = new Position(a.Axis);
for (int i = 0; i < difference.Axis.Length; i++)
{
difference.Axis[i] = a.Axis[i] - b.Axis[i];
}
return difference;
}
public float[] Axis;
}
To me this looks totally valid, so I am confused why it generates a compile time error. What should I do to solve this issue while retaining this functionality?