Compile-Time Constant Expressions for Swift

#staticAssert(
  MemoryLayout<MyStruct>.size <= 16,
  "The serialization format only has 16 bytes for MyStruct.")

I agree with @anon31136981 that #staticAssert could be spelled #assert, but it might be more useful as:

#if MemoryLayout<MyStruct>.size > 16
#error("The serialization format only has 16 bytes for MyStruct.")
#endif

For example, the standard library could use
#if Int.bitWidth == 32 instead of
#if arch(i386) || arch(arm) to conditionally support 32-bit platforms.

9 Likes