πŸ”„Conversions

Why is there no implicit conversion?

Because it would remove the type safety brought by the value objects. Let's suppose the following scenario:

builder.OfDecimal().For("Cost");
builder.OfDecimal().For("Rating");

var cost = Cost.From(12.1);
var rating = Rating.From(4.8);

if(cost >= rating)
{
    // Compiles and does not throw 
}

The preceding code compiles and does not throw. Also, a conversion that would result in an InvalidCastException should not be implicit. So it is not allowed to cast from a primitive to a value object but casting a value object to a primitive is always valid. An explicit cast is implemented.

Last updated