News for hackers. Stuff that matters.

Show HN: Zig-DbC – A design by contract library for Zig

Hi everyone,

I've made an open-source library for using design by contract (DbC) principles in the Zig programming language.

It's called Zig-DbC, and it currently provides the following features:

- A simple API to define preconditions, postconditions, and invariants.

- Contracts are active in `Debug`, `ReleaseSafe`, and `ReleaseSmall` modes to catch bugs early.

- All checks are removed at compile time in `ReleaseFast` mode for zero performance cost.

- An optional mode to handle partial state changes in functions that return errors.

- Transparent error handling that propagates errors from your code to the caller.

Project's GitHub repo: https://github.com/habedi/zig-dbc

( 11 comments | on HN )
Re:Show HN: Zig-DbC – A design by contract library for Zig by metalliqaz on Thursday August 14, 2025 @02:40PM (#44904025)
This is pretty cool, I like to see this kind of project coming to Zig. Zig's compile-time language features make DbC a natural fit.

I am not yet proficient enough in Zig to be able to evaluate how well the syntax fits into idiomatic Zig.

Good work.

Re:Show HN: Zig-DbC – A design by contract library for Zig by habedi0 on Thursday August 14, 2025 @02:51PM (#44904151)
Thanks.

I'm new to Zig myself and created this library because I had trouble verifying the correctness of the code I wrote for a set of complex data structures in another larger Zig project (this one: https://github.com/habedi/ordered). I'm currently experimenting with how to use DbC in Zig, which led to the creation of this library.

Re:Show HN: Zig-DbC – A design by contract library for Zig by skavi on Friday August 15, 2025 @02:28AM (#44909219)
Why are the checks included in ReleaseSmall?
Re:Show HN: Zig-DbC – A design by contract library for Zig by habedi0 on Friday August 15, 2025 @03:56AM (#44909701)
I created this library to help me verify the correctness of my Zig code in another project. However, I wanted the option to strip the checks when the code becomes more mature, for efficiency. For efficiency, I mainly care about speed, which is why checks are only compiled out in `ReleaseFast`.
Re:Show HN: Zig-DbC – A design by contract library for Zig by Defletter on Friday August 15, 2025 @05:39AM (#44910324)
Another project burdened by Andrew Kelley's weird hate-boner for function literals.

    (struct { fn run }).run
How ridiculous that this is required to do something so simple.
Re:Show HN: Zig-DbC – A design by contract library for Zig by ivanjermakov on Friday August 15, 2025 @08:24PM (#44918786)
Re:Show HN: Zig-DbC – A design by contract library for Zig by habedi0 on Saturday August 16, 2025 @02:59AM (#44920923)
I think they want to add lambdas to Zig at some point. The number of changes is high between releases.
Re:Show HN: Zig-DbC – A design by contract library for Zig by Defletter on Saturday August 16, 2025 @05:29AM (#44921717)
Unless something has changed from this (https://github.com/ziglang/zig/issues/1717#issuecomment-1627...), that is unlikely. The irony is that Zig's new async/await strategy is to use event loops with passed-in functions (https://www.youtube.com/watch?v=x3hOiOcbgeA&t=3643s).
Re:Show HN: Zig-DbC – A design by contract library for Zig by habedi0 on Saturday August 16, 2025 @05:43AM (#44921794)
That's kinda weird and sad. Lambdas are very useful TBH.
Re:Show HN: Zig-DbC – A design by contract library for Zig by Defletter on Saturday August 16, 2025 @06:22AM (#44921956)
Wouldn't surprise me whatsoever if someone creates zigplus, which is just some TypeScript-style superset that adds lambdas and interfaces to Zig as syntax sugar.
Re:Show HN: Zig-DbC – A design by contract library for Zig by skyzouwdev on Friday August 15, 2025 @09:00AM (#44911863)
Nice! Always thought DbC was underused outside of languages like Eiffel. Cool to see it brought to Zig with zero-cost checks in ReleaseFast — that’s a solid balance between safety and performance.