Typely
GitHub
  • Overview
    • πŸ‘Welcome!
  • Validations
    • πŸ“Common
    • πŸ”’Number
    • βš–οΈComparable
    • ✏️String
  • Customize errors
    • 🌎Localization
    • πŸ”Sensitive data
  • General
    • 🧩Class VS Struct
    • πŸ–ΌοΈSupported frameworks
    • πŸš€Benchmarks
    • ⚠️Limitations
  • Design decisions
    • πŸ”„Conversions
    • ℹ️Use of interfaces
    • πŸ€Έβ€β™‚οΈUnderlying type comparison
    • πŸ’‚Validations
Powered by GitBook
On this page
Edit on GitHub
  1. Design decisions

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.

PreviousLimitationsNextUse of interfaces

Last updated 2 years ago

πŸ”„