I writing a DSL in IronPython. Overloading operators in C# and using
them in python works fine, until you get to the assignation (=) operator.
Using the implicit cast overload solves the problem on the C# side, but it does not work in python.
This is the minimum example that reproduces the error:
class FloatValue
{
public FloatValue(float value)
{
this.value = value;
}
public static implicit operator FloatValue(float value)
{
return new FloatValue(value);
}
public float value;
}
Then I execute:
FloatValue value = 5.0f // It works!!!
But in Python:
# value is already an instance of FloatValue, it comes from somewhere. It's considered
# an immutable value, so there is no problem with generating a new instance.
value = 5.0 # Assigns the value, but does not work :(
I get the following exception:
Expected FloatValue, got float
How can I make it work?
Python does not support single-precision floating point values. The literal 5.0 is therefore (in case of IronPython) represented as a System.Double.
You can either change your DSL to use double-precision or just implicitly convert down to float by adding