Tell HN: Getting preconditions to compile in release builds of app using Swift 6

Using precondition(e) in a release build sometimes does not compile.

In some cases, using preconditionFailure() instead of precondition(expr) works.

For example, instead of precondition(v), you could try:

if !v { preconditionFailure() }

Simplifying an expression might also help. For example, precondition(a && b) could be rewritten as:

if !a { preconditionFailure() }

if !b { preconditionFailure() }

I guess the optimizer has limitations that prevent Swift 6 code, which compiles in debug builds, from always compiling in release builds.

1 points | by amichail 3 hours ago

0 comments