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

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -