1

What does the as operator expect as a right hand side argument?

and why wouldn't it accept a ClassName.self or variableName.dynamicType?

My question comes down to the difference between .self, .dynamicType and a real class name?

ielyamani
  • 17,807
  • 10
  • 55
  • 90
  • 1
    This is related to [this question](http://stackoverflow.com/questions/30905060/using-a-type-variable-in-a-generic/30905722#30905722); perhaps you will find an answer there. – jtbandes Aug 13 '15 at 02:59

1 Answers1

4

as operator expect a static Type as the right hand argument. Here the word static means something known to the compiler at compile time.

Both .self and .dynamicType are expressions, and therefore are not expected by as operator. Expressions are evaluated at runtime. In the case of .self and .dynamicType, they return a Type after evaluation.

Anyway, as operator does not expect something to be evaluated to be its right hand argument, but a "hard-coded" Type. This feature allows Swift to enforce compile time checking.

Hope this makes sense.

Fujia
  • 1,232
  • 9
  • 14