Why is Swift's fatalError's argument an @autoclosure? -
i poking around swift find out fatalerror
has signature:
@noreturn public func fatalerror(@autoclosure message: () -> string = default, file: staticstring = #file, line: uint = #line)
any specific reason why function defined way? wrong :
@noreturn public func fatalerror(message:string = default, file: staticstring = #file, line: uint = #line) { //termination code }
note understand how @autoclosure
works, , question not usage; use-cases such pattern used.
it used optionally evaluate statement reduce overhead.
for code
fatalerror("\(someexpansivecomputation())")
if function defined normal parameter passing, someexpansivecomputation()
called, in production build.
however @autoclosure
, implementation of fatalerror
can choose not call closure, avoid overhead of calling someexpansivecomputation()
a possible implementation can be
@noreturn public func fatalerror(message:string = default, file: staticstring = #file, line: uint = #line) { if debug || errorreportingenabled { log(message()) // compute message if necessary } abort() }
Comments
Post a Comment