Maybe D was wrong on contracts

H. S. Teoh hsteoh at qfbox.info
Mon Aug 17 19:23:31 UTC 2026


On Mon, Aug 17, 2026 at 06:57:04PM +0000, Quirin Schroll via Digitalmars-d wrote:
> TL;DR: D’s contracts are theoretically unsound, but there’s an easy fix.
[...]
> Maybe C++ got something right, or rather, D got something wrong that
> C++ got right-er:
> 
> *A virtual function’s postconditions don’t get limit what overriders
> do when invoked with arguments that don’t satisfy its preconditions.*
> 
> The violation of this principle might be the reason why D’s contracts
> never took off.
[...]

IMO, the reason D's contracts never took off is because they are too
complex to reason about when mixed with inheritance / polymorphism.

I do use them in my own code, but rarely in class hierarchies, if at
all.  For simple domain checking of function inputs / verification of
outputs, they can be pretty convenient for catching refactoring mistakes
that break existing code.  But mixing them with inheritance leads to
mind-boggling complexities about who/what can/cannot override
what/when/how, and what the resulting semantics are "supposed" to be --
it's just wayyy too much mental effort for marginal benefits.


T

-- 
Let X be the set not defined by this sentence...


More information about the Digitalmars-d mailing list