[swift-users] object.self?

Austin Zheng austinzheng at gmail.com
Sat Jul 9 16:48:22 CDT 2016


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".

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).

On Sat, Jul 9, 2016 at 2:27 PM, Rick Mann <rmann at latencyzero.com> wrote:

>
> > On Jul 8, 2016, at 09:45 , Austin Zheng <austinzheng at gmail.com> wrote:
> >
> > Hi Rick,
> >
> > If you have a type (let's call it "T"), you can use it two ways:
> >
> > * As a type, or part of a type, like such: "let x : T = blah()"
> > * As a value, just like any other variable, function argument, property,
> etc.
> >
> > In the second case (type-as-value), you need to append ".self" to the
> type name according to the grammar:
> >
> > "let x : Any.Type = T.self"
> >
> > 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.
> >
> > 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.
>
> Thanks, Austin. I'm familiar with all this in Swift. What threw me was
> that "subclassObject" was an instance, not a class.
>
> > 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.
>
> 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."
>
> To me, for an instance, foo an foo.self should be equivalent in all
> respects (shouldn't it?).
>
> >
> > Best,
> > Austin
> >
> >
> > On Fri, Jul 8, 2016 at 9:38 AM, Rick Mann via swift-users <
> swift-users at swift.org> wrote:
> > 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?
> >
> > for subclassObject in objects {
> >     switch subclassObject.self {        <--- Here, why not
> "subclassObject" alone?
> >     case is Subclass1:
> >         doSomethingWith(subclassObject as! Subclass1)
> >
> >     case is Subclass2:
> >         doSomethingWith(subclassObject as! Subclass2)
> >
> >     case is Subclass3:
> >         doSomethingWith(subclassObject as! Subclass3)
> >
> >     default:
> >         break
> >     }
> > }
> >
> > Thanks,
> > Rick
> >
> > > On Jul 8, 2016, at 08:15 , Dan Loewenherz via swift-users <
> swift-users at swift.org> wrote:
> > >
> > > To my knowledge, you can’t do exactly what you’re trying to do, but
> this is close:
> > >
> > > for subclassObject in objects {
> > >     switch subclassObject.self {
> > >     case is Subclass1:
> > >         doSomethingWith(subclassObject as! Subclass1)
> > >
> > >     case is Subclass2:
> > >         doSomethingWith(subclassObject as! Subclass2)
> > >
> > >     case is Subclass3:
> > >         doSomethingWith(subclassObject as! Subclass3)
> > >
> > >     default:
> > >         break
> > >     }
> > > }
> > >
> > > On Fri, Jul 8, 2016 at 10:11 AM, Nate Birkholz via swift-users <
> swift-users at swift.org> wrote:
> > > This looks like it doesn't work (swift 2.x), but wanted to be sure
> it's not supported:
> > > class Superclass {}
> > > class Subclass1 : Superclass {}
> > > class Subclass2 : Superclass {}
> > > class Subclass3 : Superclass {}
> > >
> > > let sc1 = Subclass1()
> > > let sc2 = Subclass2()
> > > let sc3 = Subclass3()
> > >
> > > let objects : [Superclass] = [sc1, sc2, sc3]
> > >
> > > for subclassObject in objects {
> > >     switch subclassObject {
> > >     case let object = subclassObject as? Subclass1:
> > >         doSomethingWith(object)
> > >     case let object = subclassObject as? Subclass2:
> > >         doSomethingWith(object)
> > >     case let object = subclassObject as? Subclass3:
> > >         doSomethingWith(object)
> > >     default:
> > >         return
> > >     }
> > > }
> > >
> > > This gives an error, expecting a colon (:) after object on every case.
> > >
> > > 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.
> > >
> > >
> > > --
> > > Nate Birkholz
> > >
> > > _______________________________________________
> > > swift-users mailing list
> > > swift-users at swift.org
> > > https://lists.swift.org/mailman/listinfo/swift-users
> > >
> > >
> > > _______________________________________________
> > > swift-users mailing list
> > > swift-users at swift.org
> > > https://lists.swift.org/mailman/listinfo/swift-users
> >
> >
> > --
> > Rick Mann
> > rmann at latencyzero.com
> >
> >
> > _______________________________________________
> > swift-users mailing list
> > swift-users at swift.org
> > https://lists.swift.org/mailman/listinfo/swift-users
> >
>
>
> --
> Rick Mann
> rmann at latencyzero.com
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160709/c5ccb254/attachment.html>


More information about the swift-users mailing list