Exceptions with guard-clause as shorthand instead of do/try/catch


(Matis Schotte) #1

Classification: Public

Hello,

I stumbled upon the exception handling and the quite practical guard
keyword for early-exiting a function.
Unfortunately, the try? discards an exception, if there is one.

What I am interested in is to use the shorter guard-style instead of the
lengthy do/try/catch-approach, example follows:

Current situation is (example #1):
let smth: AnyObject?
do {
    smth = try myThrowingFunc()
} catch let error {
    print(error) // here I can access the exception, but this code is
quite big and not very intuitive, and the result will always be an
optional
    return
}

I would like to use it like this(example #2):
guard let smth = try? myThrowingFunc() else {
    print(error) // in swift 2.2 the exception is discarded, so no access
possible, but this code is much cleaner
    return
}

Advantages are better readability because of shorter code, and to avoid
the optionals from ex.#1 and therefore alot of ! and ? afterwards. In ex#2
I either have the smth constant, or the function stops and returns. In
ex#1, I will have an optional with a value, which again needs a few
additional steps to unwrap and use.

So I would love the ability to access exceptions (maybe with a standard
variable name) inside a guard-clause

I have created a question on stackoverflow here:
http://stackoverflow.com/questions/35776383
But have answered that already myself. I would love to get some feedback
from you guys, maybe you have some other tips for me.

Kind regards,
Matis Schotte

···

____________________________________________________

Matis Schotte
Mobile Solutions | Software Engineer

Deutsche Bank AG
Theodor-Heuss-Allee 72, 60486 Frankfurt am Main, Germany
Email matis.schotte@db.com

--

Informationen (einschließlich Pflichtangaben) zu einzelnen, innerhalb der EU tätigen Gesellschaften und Zweigniederlassungen des Konzerns Deutsche Bank finden Sie unter http://www.deutsche-bank.de/de/content/pflichtangaben.htm. Diese E-Mail enthält vertrauliche und/ oder rechtlich geschützte Informationen. Wenn Sie nicht der richtige Adressat sind oder diese E-Mail irrtümlich erhalten haben, informieren Sie bitte sofort den Absender und vernichten Sie diese E-Mail. Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser E-Mail ist nicht gestattet.

Please refer to http://www.db.com/en/content/eu_disclosures.htm for information (including mandatory corporate particulars) on selected Deutsche Bank branches and group companies registered or incorporated in the European Union. This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and delete this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden.


#2

This was discussed here a couple days ago:

http://thread.gmane.org/gmane.comp.lang.swift.evolution/8266

- Stephen

···

On Mar 3, 2016, at 11:58 AM, Matis Schotte via swift-evolution <swift-evolution@swift.org> wrote:

What I am interested in is to use the shorter guard-style instead of the lengthy do/try/catch-approach, example follows:


(Matis Schotte) #3

Classification: Public

Thank you for the link, I am glad that there seems to be interest in that
topic.
My +1 for this feature.

Kind regards,
Matis Schotte

···

____________________________________________________

This was discussed here a couple days ago:

http://thread.gmane.org/gmane.comp.lang.swift.evolution/8266

- Stephen

--

Informationen (einschließlich Pflichtangaben) zu einzelnen, innerhalb der EU tätigen Gesellschaften und Zweigniederlassungen des Konzerns Deutsche Bank finden Sie unter http://www.deutsche-bank.de/de/content/pflichtangaben.htm. Diese E-Mail enthält vertrauliche und/ oder rechtlich geschützte Informationen. Wenn Sie nicht der richtige Adressat sind oder diese E-Mail irrtümlich erhalten haben, informieren Sie bitte sofort den Absender und vernichten Sie diese E-Mail. Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser E-Mail ist nicht gestattet.

Please refer to http://www.db.com/en/content/eu_disclosures.htm for information (including mandatory corporate particulars) on selected Deutsche Bank branches and group companies registered or incorporated in the European Union. This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and delete this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden.


(Jacob Bandes-Storch) #4

I'm glad that there is interest too!

I'm planning on taking Chris's feedback into account and writing up a more
detailed proposal in the near future, I just haven't had time yet.

Jacob

···

On Thu, Mar 3, 2016 at 9:16 AM, Matis Schotte via swift-evolution < swift-evolution@swift.org> wrote:

Classification: Public

Thank you for the link, I am glad that there seems to be interest in that
topic.
My +1 for this feature.

Kind regards,
Matis Schotte

____________________________________________________

This was discussed here a couple days ago:

http://thread.gmane.org/gmane.comp.lang.swift.evolution/8266

- Stephen

--

Informationen (einschließlich Pflichtangaben) zu einzelnen, innerhalb der
EU tätigen Gesellschaften und Zweigniederlassungen des Konzerns Deutsche
Bank finden Sie unter
http://www.deutsche-bank.de/de/content/pflichtangaben.htm. Diese E-Mail
enthält vertrauliche und/ oder rechtlich geschützte Informationen. Wenn Sie
nicht der richtige Adressat sind oder diese E-Mail irrtümlich erhalten
haben, informieren Sie bitte sofort den Absender und vernichten Sie diese
E-Mail. Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser
E-Mail ist nicht gestattet.

Please refer to http://www.db.com/en/content/eu_disclosures.htm for
information (including mandatory corporate particulars) on selected
Deutsche Bank branches and group companies registered or incorporated in
the European Union. This e-mail may contain confidential and/or privileged
information. If you are not the intended recipient (or have received this
e-mail in error) please notify the sender immediately and delete this
e-mail. Any unauthorized copying, disclosure or distribution of the
material in this e-mail is strictly forbidden.

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