-2

In Swift I can use as? to downcast a type to another type. If the casting fails then nil is returned.

If I use as! and the casting fails then an error is thrown.

Why do we have as? and as!? Why not just as like C# and other languages. If the casting fails just return null and that's it!

Matt Clark
  • 27,671
  • 19
  • 68
  • 123
john doe
  • 9,220
  • 23
  • 91
  • 167
  • 2
    Swift is much stricter about types and type conversions that most other languages. As you say, "as?" will downcast if it can, and return nil if not. (It returns an optional.) The "as!" syntax is known as the force-downcast operator, that will throw a runtime exception if it fails. – Duncan C Aug 18 '15 at 17:46
  • 1
    This is a completely different question. The question does not asks for the difference between as? and as! it asks the reason why not only "as" and why the need of "as?" and "as!" – john doe Aug 18 '15 at 17:47
  • 3
    Because sometimes you don't know whether the test will fail (and therefore need the optional `as?`) and sometimes you know it won't fail (and therefore you do not want to return an optional so you'd use `as!`). – Rob Aug 18 '15 at 17:47
  • 1
    Nominated to reopen. This is not a question about downcasting optionals, or what downcasting does, this is a question about why there are two downcast operators. – Catfish_Man Aug 18 '15 at 17:47
  • @Rob But if we have "as" then if null is returned then we know that it has failed. There is no need to confuse the developer in predicting which one will fail and which will not. Just return null if it fails period! – john doe Aug 18 '15 at 17:49
  • 1
    1. sometimes you want the system crash when casting fail, and start debug 2. I believe it helps most developer to debug – Justus Jianxing Zhang Aug 18 '15 at 18:08
  • 2
    @JohnDoe - If that's the behavior you want, then just use `as?` syntax and use the optional return value. But, if the developer knows that, having gotten to that point in the code, that a particular conversion will always succeed, why must they be forced to use optional values? Sometimes you know for a fact that a conversion will never fail, in which case it's better programming style to not use optionals in that scenario. – Rob Aug 18 '15 at 18:08
  • 1
    It goes beyond what the programmer knows to what the programmer wants to communicate. `as?` means "I'll deal with what comes"; `as!` means "You're responsible for giving me the correct thing". – Tommy Aug 18 '15 at 20:00
  • Voting to close again. If you understand how the language works (i.e. what the difference is between `as!` and `as?` then this is like asking why a football field is 100 yards long. It's because that's Swift. Why Swift is designed that way is a matter of opinion, and thus is not a Stack Overflow topic. – matt Aug 18 '15 at 20:01

1 Answers1

1

For convenience.

The as? operator is needed in order to provide a downcast that can fail gracefully. The as! operator is then a short form of writing something like (value as? Type)!.

You could live without as! if you just had as?, but as! is more convenient than the code I typed above. Prior to Swift 1.2, as! was as, which led to confusion with another use of as. The move to as! makes its use clear, and is in line with the ? and ! used for optionals.

In Swift, you can't "just return null and that's it" because values don't support nil or null values by default. You need to use optionals to explicitly indicate the potential for an absence of a value, which is a language choice intended to increase safety. With an optional return, you need to extract the value at some point to work with it, thus the need for as! or a ! when you are certain that the downcast will succeed.

Brad Larson
  • 170,088
  • 45
  • 397
  • 571