Back to articles
I Built a Type-Safe SI Unit Library in Swift — And the Compiler Catches Your Physics Mistakes

I Built a Type-Safe SI Unit Library in Swift — And the Compiler Catches Your Physics Mistakes

via Dev.toHenrique Sasaki Yuya

You've seen this bug before: let speed = distance + time // Compiles. Runs. Produces nonsense. Or worse — the Mars Climate Orbiter kind, where pound-seconds and newton-seconds silently mix, and a $327 million spacecraft burns up in the atmosphere. I built SystemeInternational to make that class of bug extinct in Swift — at compile time, with zero runtime cost. What If the Type System Knew Physics? SystemeInternational encodes physical dimensions, units, and even the distinction between absolute positions and intervals into Swift's type parameters. Every quantity is: Quantity < Scalar , Unit , Space > That's it. 8 bytes. The Unit and Space are phantom types — they exist only at compile time and vanish completely in the binary. import UnitesSI let distance = try Quantity < Double , Kilometer > ( 36 ) let time = try Quantity < Double , Second > ( 3_600 ) let speed = try distance / time // ✅ Compiles — dimension is Length/Time let nonsense = try distance + time // ❌ Compile error — no over

Continue reading on Dev.to

Opens in a new tab

Read Full Article
0 views

Related Articles