<div dir="ltr">Yes, as far as I know 'foo' and 'foo.self' are equivalent. I don't actually know why the latter exists, except in analogy to "T.self".<div><br></div><div>There was a mistake in my response; the metatype of 'foo' is not 'foo.self', it is 'foo.dynamicType' (or whatever new form dynamicType is going to take in Swift 3).</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Jul 9, 2016 at 2:27 PM, Rick Mann <span dir="ltr"><<a href="mailto:rmann@latencyzero.com" target="_blank">rmann@latencyzero.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class=""><br>
> On Jul 8, 2016, at 09:45 , Austin Zheng <<a href="mailto:austinzheng@gmail.com">austinzheng@gmail.com</a>> wrote:<br>
><br>
> Hi Rick,<br>
><br>
> If you have a type (let's call it "T"), you can use it two ways:<br>
><br>
> * As a type, or part of a type, like such: "let x : T = blah()"<br>
> * As a value, just like any other variable, function argument, property, etc.<br>
><br>
> In the second case (type-as-value), you need to append ".self" to the type name according to the grammar:<br>
><br>
> "let x : Any.Type = T.self"<br>
><br>
> 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.<br>
><br>
> 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.<br>
<br>
</span>Thanks, Austin. I'm familiar with all this in Swift. What threw me was that "subclassObject" was an instance, not a class.<br>
<span class=""><br>
> 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.<br>
<br>
</span>I would have thought so, but the response to this answer was something along the lines of "I never know when to use the object or its self."<br>
<br>
To me, for an instance, foo an foo.self should be equivalent in all respects (shouldn't it?).<br>
<div class="HOEnZb"><div class="h5"><br>
><br>
> Best,<br>
> Austin<br>
><br>
><br>
> On Fri, Jul 8, 2016 at 9:38 AM, Rick Mann via swift-users <<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>> wrote:<br>
> 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>
><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>
><br>
<br>
<br>
</div></div><span class="HOEnZb"><font color="#888888">--<br>
Rick Mann<br>
<a href="mailto:rmann@latencyzero.com">rmann@latencyzero.com</a><br>
<br>
<br>
</font></span></blockquote></div><br></div>