When and how to use @noreturn attribute in Swift? -


i read code block enclosed in curly braces after keyword else in context of guard-else flow, must call function marked noreturn attribute or transfer control using return, break, continue or throw.

the last part quite clear, while don't understand first.

first of all, function returns (an empty tuple @ least) if don't declare return type. secondly, when can use noreturn function? docs suggesting core, built-in methods marked noreturn?

the else clause of guard statement required, , must either call function marked noreturn attribute or transfer program control outside guard statement’s enclosing scope using 1 of following statements:

return  break  continue  throw 

here source.

first of all, function returns (an empty tuple @ least) if don't declare return type.

(@noreturn obsolete; see swift 3 update below.) no, there functions terminate process , not return caller. these marked in swift @noreturn, such as

@noreturn public func fatalerror(@autoclosure message: () -> string = default, file: staticstring = #file, line: uint = #line) @noreturn public func preconditionfailure(@autoclosure message: () -> string = default, file: staticstring = #file, line: uint = #line) @noreturn public func abort() @noreturn public func exit(_: int32) 

and there may more.

(remark: similar annotations exist in other programming languages or compilers, such [[noreturn]] in c++11, __attribute__((noreturn)) gcc extension, or _noreturn clang compiler.)

you can mark own function @noreturn if terminates process unconditionally, e.g. calling 1 of built-in functions, such as

@noreturn func myfatalerror() {     // else , ...     fatalerror("something went wrong!") } 

now can use function in else clause of guard statement:

guard let n = int("1234") else { myfatalerror() } 

@noreturn functions can used mark cases "should not occur" , indicate programming error. simple example (an extract missing return uitableviewcell):

override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell {     let cell: mytableviewcell      switch (indexpath.row) {     case 0:         cell = tableview.dequeuereusablecellwithidentifier("cell0", forindexpath: indexpath) as! mytableviewcell         cell.backgroundcolor = uicolor.greencolor()     case 1:         cell = tableview.dequeuereusablecellwithidentifier("cell1", forindexpath: indexpath) as! mytableviewcell         cell.backgroundcolor = uicolor.redcolor()     default:         myfatalerror()     }     // setup other cell properties ...     return cell } 

without myfatalerror() marked @noreturn, compiler complain missing return in default case.


update: in swift 3 (xcode 8 beta 6) @noreturn attribute has been replaced never return type, above example written as

func myfatalerror() -> never  {     // else , ...     fatalerror("something went wrong!") } 

Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -