[swift-users] Why can't Swift instance methods call class methods without qualification?
zh ao
owenzx at gmail.com
Thu Jun 30 23:07:19 CDT 2016
Swift has its own suggested patterns on calling methods. For example, it
suggests you calling instance methods directly inside class. It also forces
you calling self.method() in closure that without @noescape. As well as the
static method with the class name in your question. Those are different
from C++. But it does differ the differences between methods.
Zhaoxin
On Fri, Jul 1, 2016 at 10:01 AM, Rick Mann <rmann at latencyzero.com> wrote:
>
> > On Jun 30, 2016, at 18:47 , zh ao <owenzx at gmail.com> wrote:
> >
> > Just a choice made by the language designers to distinguish the call at
> the call site.
> >
> > You should be aware of using static methods as it may change static
> variables, which affects all instances of that class. Normally I think
> static methods is designed to use outside the class instance, if you have
> to use it inside its instance method. You may need to rethink the pattern
> you do.
>
> I think of static methods as applying to all instances, and so code them
> to "behave properly" no matter how they're called (since I can't really
> control who calls it, unless it's my own class). Since it's implicitly
> obvious (to me) that I mean "this class" when I call one, I find it a bit
> tedious qualify the call with the class name.
>
> It's fine, in the end, but I was curious why it was like this, and if
> there was a more obvious reason why it needed the qualification.
>
> >
> > Zhaoxin
> >
> > On Fri, Jul 1, 2016 at 8:59 AM, Rick Mann via swift-users <
> swift-users at swift.org> wrote:
> > Why can my instance methods not call class methods without the class
> specifier?
> >
> > class MyClass
> > {
> > func
> > foo()
> > {
> > classMethod()
> > }
> >
> > class
> > func
> > classMethod()
> > {
> > }
> > }
> >
> > Why do I have to call MyClass.classMethod()? Just a choice made by the
> language designers to distinguish the call at the call site? I like C++'s
> way of treating all static methods as directly available to the instance.
> >
> > --
> > 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/20160701/e23fee13/attachment.html>
More information about the swift-users
mailing list