<div dir="ltr">Hi Rick,<div><br></div><div>If you have a type (let's call it "T"), you can use it two ways:</div><div><br></div><div>* As a type, or part of a type, like such: "let x : T = blah()"</div><div>* As a value, just like any other variable, function argument, property, etc.</div><div><br></div><div>In the second case (type-as-value), you need to append ".self" to the type name according to the grammar:</div><div><br></div><div>"let x : Any.Type = T.self"</div><div><br></div><div>There was a "bug" in Swift 2.x where you could sometimes use just "T", without the ".self", in certain cases (in particular, when you were passing in a type-as-value to a function with one unlabeled argument). That bug has since been fixed.</div><div><br></div><div>As for types-as-values: Swift allows you to treat a type as a normal value, which means you can do whatever you want with it: pass it to functions and return it from functions, store it in properties or variables, etc. If you have one of these types-as-values (called 'metatypes'), you can do certain things like call static methods or initializers on them, use them to parameterize generic functions, etc.</div><div><br></div><div>However, to get back to your original question, the `.self` in that switch statement actually isn't necessary and you should really just be switching on the value of subclassObject itself, not the value of its type.</div><div> <br></div><div>Best,</div><div>Austin</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jul 8, 2016 at 9:38 AM, Rick Mann via swift-users <span dir="ltr"><<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I just saw a question which brought up something I didn't know about. Apparently sometimes you have to call object.self in a place that looks like you should just use "object." What does this usage mean?<br>
<br>
for subclassObject in objects {<br>
switch subclassObject.self { <--- Here, why not "subclassObject" alone?<br>
case is Subclass1:<br>
doSomethingWith(subclassObject as! Subclass1)<br>
<br>
case is Subclass2:<br>
doSomethingWith(subclassObject as! Subclass2)<br>
<br>
case is Subclass3:<br>
doSomethingWith(subclassObject as! Subclass3)<br>
<br>
default:<br>
break<br>
}<br>
}<br>
<br>
Thanks,<br>
Rick<br>
<br>
> On Jul 8, 2016, at 08:15 , Dan Loewenherz via swift-users <<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>> wrote:<br>
><br>
> To my knowledge, you can’t do exactly what you’re trying to do, but this is close:<br>
><br>
> for subclassObject in objects {<br>
> switch subclassObject.self {<br>
> case is Subclass1:<br>
> doSomethingWith(subclassObject as! Subclass1)<br>
><br>
> case is Subclass2:<br>
> doSomethingWith(subclassObject as! Subclass2)<br>
><br>
> case is Subclass3:<br>
> doSomethingWith(subclassObject as! Subclass3)<br>
><br>
> default:<br>
> break<br>
> }<br>
> }<br>
><br>
> On Fri, Jul 8, 2016 at 10:11 AM, Nate Birkholz via swift-users <<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>> wrote:<br>
> This looks like it doesn't work (swift 2.x), but wanted to be sure it's not supported:<br>
> class Superclass {}<br>
> class Subclass1 : Superclass {}<br>
> class Subclass2 : Superclass {}<br>
> class Subclass3 : Superclass {}<br>
><br>
> let sc1 = Subclass1()<br>
> let sc2 = Subclass2()<br>
> let sc3 = Subclass3()<br>
><br>
> let objects : [Superclass] = [sc1, sc2, sc3]<br>
><br>
> for subclassObject in objects {<br>
> switch subclassObject {<br>
> case let object = subclassObject as? Subclass1:<br>
> doSomethingWith(object)<br>
> case let object = subclassObject as? Subclass2:<br>
> doSomethingWith(object)<br>
> case let object = subclassObject as? Subclass3:<br>
> doSomethingWith(object)<br>
> default:<br>
> return<br>
> }<br>
> }<br>
><br>
> This gives an error, expecting a colon (:) after object on every case.<br>
><br>
> I wanted to be sure I wasn't missing something in my syntax (nor some obvious-to-others reason this isn't supported) before going to swift evolution.<br>
><br>
><br>
> --<br>
> Nate Birkholz<br>
><br>
> _______________________________________________<br>
> swift-users mailing list<br>
> <a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
> <a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br>
><br>
><br>
> _______________________________________________<br>
> swift-users mailing list<br>
> <a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
> <a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br>
<span class="HOEnZb"><font color="#888888"><br>
<br>
--<br>
Rick Mann<br>
<a href="mailto:rmann@latencyzero.com">rmann@latencyzero.com</a><br>
<br>
<br>
_______________________________________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br>
</font></span></blockquote></div><br></div>