@noReturn


(Rien) #1

Is there any way to mark a function as “no return”?

Reason: The compiler generates an error when the else block from a guard does not terminate the execution by either a return or a fatalError. I want to call out to a function and raise the fatalError in that function.

func findReasonAndTerminate() {
   let reason: String = ….
   fatalError(reason)
}

main.swift:

guard let data = buildData() else { findReasonAndTerminate() }

Currently the work around is to add another fatalError like this:

guard let data = buildData() else { findReasonAndTerminate(); fatalError }

but it would be nice to have some attribute like @noReturn:

@noReturn
func findReasonAndTerminate() { … }

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: http://github.com/Balancingrock
Project: http://swiftfire.nl


(Philip Erickson) #2

I believe returning Never does what you want, e.g.

import Foundation

func findReasonAndTerminate() -> Never
{
   let reason: String = findReason()
   fatalError(reason)
}

func findReason() -> String
{
return "some reason"
}

func buildData() -> Data?
{
return nil
}

guard let data = buildData() else { findReasonAndTerminate() }

···

On Fri, Mar 24, 2017 at 3:02 AM, Rien via swift-users <swift-users@swift.org > wrote:

Is there any way to mark a function as “no return”?

Reason: The compiler generates an error when the else block from a guard
does not terminate the execution by either a return or a fatalError. I want
to call out to a function and raise the fatalError in that function.

func findReasonAndTerminate() {
   let reason: String = ….
   fatalError(reason)
}

main.swift:

guard let data = buildData() else { findReasonAndTerminate() }

Currently the work around is to add another fatalError like this:

guard let data = buildData() else { findReasonAndTerminate(); fatalError }

but it would be nice to have some attribute like @noReturn:

@noReturn
func findReasonAndTerminate() { … }

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: http://github.com/Balancingrock
Project: http://swiftfire.nl

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Rien) #3

Excellent!

Love this list :wink:

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: http://github.com/Balancingrock
Project: http://swiftfire.nl

···

On 24 Mar 2017, at 10:12, Philip Erickson <philiperickson@gmail.com> wrote:

I believe returning Never does what you want, e.g.

import Foundation

func findReasonAndTerminate() -> Never
{
   let reason: String = findReason()
   fatalError(reason)
}

func findReason() -> String
{
  return "some reason"
}

func buildData() -> Data?
{
  return nil
}

guard let data = buildData() else { findReasonAndTerminate() }

On Fri, Mar 24, 2017 at 3:02 AM, Rien via swift-users <swift-users@swift.org> wrote:
Is there any way to mark a function as “no return”?

Reason: The compiler generates an error when the else block from a guard does not terminate the execution by either a return or a fatalError. I want to call out to a function and raise the fatalError in that function.

func findReasonAndTerminate() {
   let reason: String = ….
   fatalError(reason)
}

main.swift:

guard let data = buildData() else { findReasonAndTerminate() }

Currently the work around is to add another fatalError like this:

guard let data = buildData() else { findReasonAndTerminate(); fatalError }

but it would be nice to have some attribute like @noReturn:

@noReturn
func findReasonAndTerminate() { … }

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: http://github.com/Balancingrock
Project: http://swiftfire.nl

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Zhao Xin) #4

Change you `func findReasonAndTerminate()` to `func
findReasonAndTerminate() -> String`.
Then you can use `fatalError(func findReasonAndTerminate())`.

Zhaoxin

···

On Fri, Mar 24, 2017 at 5:02 PM, Rien via swift-users <swift-users@swift.org > wrote:

Is there any way to mark a function as “no return”?

Reason: The compiler generates an error when the else block from a guard
does not terminate the execution by either a return or a fatalError. I want
to call out to a function and raise the fatalError in that function.

func findReasonAndTerminate() {
   let reason: String = ….
   fatalError(reason)
}

main.swift:

guard let data = buildData() else { findReasonAndTerminate() }

Currently the work around is to add another fatalError like this:

guard let data = buildData() else { findReasonAndTerminate(); fatalError }

but it would be nice to have some attribute like @noReturn:

@noReturn
func findReasonAndTerminate() { … }

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: http://github.com/Balancingrock
Project: http://swiftfire.nl

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Zhao Xin) #5

The last line should be `fatalError(findReasonAndTerminate())`.

Zhaoxin

···

On Fri, Mar 24, 2017 at 5:13 PM, Zhao Xin <owenzx@gmail.com> wrote:

Change you `func findReasonAndTerminate()` to `func
findReasonAndTerminate() -> String`.
Then you can use `fatalError(func findReasonAndTerminate())`.

Zhaoxin

On Fri, Mar 24, 2017 at 5:02 PM, Rien via swift-users < > swift-users@swift.org> wrote:

Is there any way to mark a function as “no return”?

Reason: The compiler generates an error when the else block from a guard
does not terminate the execution by either a return or a fatalError. I want
to call out to a function and raise the fatalError in that function.

func findReasonAndTerminate() {
   let reason: String = ….
   fatalError(reason)
}

main.swift:

guard let data = buildData() else { findReasonAndTerminate() }

Currently the work around is to add another fatalError like this:

guard let data = buildData() else { findReasonAndTerminate(); fatalError }

but it would be nice to have some attribute like @noReturn:

@noReturn
func findReasonAndTerminate() { … }

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: http://github.com/Balancingrock
Project: http://swiftfire.nl

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Saagar Jha) #6

By the way, return Never used to be called @noreturn, so you weren't far
off!

···

On Fri, Mar 24, 2017 at 02:22 Rien via swift-users <swift-users@swift.org> wrote:

Excellent!

Love this list :wink:

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: http://github.com/Balancingrock
Project: http://swiftfire.nl

> On 24 Mar 2017, at 10:12, Philip Erickson <philiperickson@gmail.com> > wrote:
>
> I believe returning Never does what you want, e.g.
>
>
> import Foundation
>
> func findReasonAndTerminate() -> Never
> {
> let reason: String = findReason()
> fatalError(reason)
> }
>
> func findReason() -> String
> {
> return "some reason"
> }
>
> func buildData() -> Data?
> {
> return nil
> }
>
> guard let data = buildData() else { findReasonAndTerminate() }
>
>
> On Fri, Mar 24, 2017 at 3:02 AM, Rien via swift-users < > swift-users@swift.org> wrote:
> Is there any way to mark a function as “no return”?
>
> Reason: The compiler generates an error when the else block from a guard
does not terminate the execution by either a return or a fatalError. I want
to call out to a function and raise the fatalError in that function.
>
> func findReasonAndTerminate() {
> let reason: String = ….
> fatalError(reason)
> }
>
> main.swift:
>
> guard let data = buildData() else { findReasonAndTerminate() }
>
>
> Currently the work around is to add another fatalError like this:
>
> guard let data = buildData() else { findReasonAndTerminate(); fatalError
}
>
>
> but it would be nice to have some attribute like @noReturn:
>
> @noReturn
> func findReasonAndTerminate() { … }
>
>
> Regards,
> Rien
>
> Site: http://balancingrock.nl
> Blog: http://swiftrien.blogspot.com
> Github: http://github.com/Balancingrock
> Project: http://swiftfire.nl
>
>
>
>
>
> _______________________________________________
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
>

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

--
-Saagar Jha