Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.objective-c > #239

Re: A question on designated initializers

From Jon Rossen <jonr17@comcast.net>
Newsgroups comp.lang.objective-c
Subject Re: A question on designated initializers
Date 2015-11-25 17:30 -0800
Organization albasani.net
Message-ID <n35nc1$48e$1@news.albasani.net> (permalink)
References (5 earlier) <87oaeivr7a.fsf@kuiper.lan.informatimago.com> <n337jb$6mt$2@dont-email.me> <87d1uyvj8a.fsf@kuiper.lan.informatimago.com> <n33r9o$27f$1@news.albasani.net> <878u5muslu.fsf@kuiper.lan.informatimago.com>

Show all headers | View raw


Pascal,

I'm starting to get hopelessly confused.
The following are parts of your response that I think address where I 
was (or still am) confused


> - if there is no method for the selector in that class, then search in
>    the superclass, and so on.

Right, I'm generally already familiar with the search pattern here.


> When we send a message to super, we start the search directly from the
> superclass, instead of starting from the class.

Ok, I think this may be were my problem has been.  You are saying here 
that if you send a message to super, then the *super class* is where the 
search begins, *not* from the current class.  This seems to put a 
different spin on what you said just above, where the search starts in 
the current class.



> So when a method is found, such as -[SomeClass designatedInitializer:]
> then it is called.  A method is basically a C function with two
> additionnal parameters (a selector _cmd, and the recipient self).
>
> Notice that methods are denoted by:
>
> - a sign - for instance methods (stored in the class), + for class
>    methods (stored in the metaclass).
> - a class name,
> - a designator.
>
>    -[SomeClass designatedInitializer:]
>
>
> So you can have two methods:
>
>    -[MyClass designatedInitializer:]
>    -[MySubClass designatedInitializer:]

Ok, got it.  Just a nitpick...these are instance methods, not class 
methods so technically shouldn't this example be something like:
[myClassObject designatedInitializer]
[mySubclassObject designatedInitializer]

...or does the '-' you have as a prefix serve to state this and the 
examples (names used) are supposed to be taken abstractly?


> and when you send a designatedInitializer: message:
>
>     [self designatedInitializer:42]
>
> one or the other method may be called first, depending on the class of
> self.

The version that is called first is the version that is in self's class. 
  Please tell me that what I have said is correct, or else I'm going to 
kill myself after I destroy my computer.  :-)


> The class of self is not necessarily the class of the method being
> executed!

This statement seems at odds at the one right above that I commented on. 
  What are you saying here?  You seem to be saying that the class of 
self may not be the class of the method being executed.  But I think I 
know why you are saying this.  You seem to be implying something here 
but you are not explicitly giving a specific scenario.

Is this statement related to the fact that the search for the method 
executed first could be in *super's* class?  This is what you touched on 
a few comments above.  In other words in that scenario (if I'm 
understanding that correctly), a different method than the one in self's 
class would be executed.  Is this what you are saying here?


> Usually, the class of self is a SUBCLASS of the class of the method
> being executed.

Ok, this is the scenario that is at the heart of what I was saying just 
directly above, or at least trying to.





>
> The problem with Don's code is that there are four methods:
>
>    -[MyClass init]
>        [self designatedInitializer:default]
>
>    -[MyClass designatedInitializer:]
>        …
>
>    -[MySubClass init]
>        [super init]
>
>    -[MySubClass designatedInitializer:]
>        [super designatedInitializer:default]
>
>
> [MySubclass alloc] returns an instance of MySubClass.
> The only instance we'll have is of the class MySubClass.
>
> Sending the message [[MySubclass alloc] init]
>
> sends the init message to an instance of the class MySubClass.
>
> So we call the method -[MySubClass init], passing the instance of the
> class MySubClass.
>
> which sends the message init to super, that is, to the same instance of
> the class MySubClass, but searching the method -init starting from the
> superclass.
>
> So we call the method -[MyClass init], passing the instance of the
> class MySubClass.
>
> This sends the designatedInitializer: message to this instance of the
> class MySubClass.
>
> So we search a -designatedInitializer: method starting from the class
> MySubClass, and we find -[MySubClass designatedInitializer:]
>
> This sends the message [super init], so we search an -init method from
> the superclass and find -[MyClass init], passing the instance of the
> class MySubClass.
>
> This sends the designatedInitializer: message to this instance of the
> class MySubClass.
>
> So we search a -designatedInitializer: method starting from the class
> MySubClass, and we find -[MySubClass designatedInitializer:]
>
> This sends the message [super init], so we search an -init method from
> the superclass and find -[MyClass init], passing the instance of the
> class MySubClass.
>
> This sends the designatedInitializer: message to this instance of the
> class MySubClass.
>
> So we search a -designatedInitializer: method starting from the class
> MySubClass, and we find -[MySubClass designatedInitializer:]
>
> This sends the message [super init], so we search an -init method from
> the superclass and find -[MyClass init], passing the instance of the
> class MySubClass.
>
> This sends the designatedInitializer: message to this instance of the
> class MySubClass.
>
> So we search a -designatedInitializer: method starting from the class
> MySubClass, and we find -[MySubClass designatedInitializer:]
>
> This sends the message [super init], so we search an -init method from
> the superclass and find -[MyClass init],
>
> etc.
>
>
>
>
> The whole point of OOP and inheritance, is to be able send a message to
> an object without knowing at compilation time what class or subclass
> this object will be.  So the actual method called when we send this
> message, is determined at run-time, and may be a method of a subclass
> that wasn't even imagined when we wrote this message sending.
>
> When we have:
>
>      @implementation MyClass
>      -(id)init{
>         return [self designatedInitializer:42];
>      }
>      @end
>
> the only raison-d'être of this method, is to call the method of the
> subclass of MyClass of which self is an instance.
>
> Because we know, when we write MyClass, that we don't know what the
> future has in stock for us.  We know that the customer will come with
> some crazy requirements, which makes -[MyClass designatedInitializer:]
> completely inadapted and outdated.
>
> Therefore when the crazy customer comes with his crazy requirements, we
> will write a subclass that will do the right thing, by implementing a
> -[CrazyCustomerSubclass designatedInitializer:] method, knowing that the
> rest of the MyClass code will still work, because everytime it has to
> rely on some specific behavior, it will send a message to the object
> that will be dispatched to the right method according to the class of
> that object.
>

Back to comp.lang.objective-c | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

A question on designated initializers Jon Rossen <jonr17@comcast.net> - 2015-11-22 13:39 -0800
  Re: A question on designated initializers Jon Rossen <jonr17@comcast.net> - 2015-11-22 14:48 -0800
    Re: A question on designated initializers Louis Wu <louiswu@ringworld.net> - 2015-11-22 16:26 -0800
      Re: A question on designated initializers Jon Rossen <jonr17@comcast.net> - 2015-11-22 17:24 -0800
        Re: A question on designated initializers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-11-23 02:39 +0100
  Re: A question on designated initializers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-11-23 01:18 +0100
    Re: A question on designated initializers Jon Rossen <jonr17@comcast.net> - 2015-11-22 16:49 -0800
      Re: A question on designated initializers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-11-23 02:02 +0100
        Re: A question on designated initializers Jon Rossen <jonr17@comcast.net> - 2015-11-27 17:19 -0800
          Re: A question on designated initializers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-11-28 13:22 +0100
            Re: A question on designated initializers Jon Rossen <jonr17@comcast.net> - 2015-11-28 17:37 -0800
              Re: A question on designated initializers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-11-29 03:45 +0100
  Re: A question on designated initializers Don Bruder <dakidd@sonic.net> - 2015-11-22 19:15 -0800
    Re: A question on designated initializers Jon Rossen <jonr17@comcast.net> - 2015-11-23 18:44 -0800
      Re: A question on designated initializers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-11-24 04:58 +0100
        Re: A question on designated initializers Jon Rossen <jonr17@comcast.net> - 2015-11-24 16:42 -0800
          Re: A question on designated initializers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-11-25 02:00 +0100
        Re: A question on designated initializers Greg Parker <gparker@apple.com> - 2015-11-25 01:25 -0800
      Re: A question on designated initializers Don Bruder <dakidd@sonic.net> - 2015-11-24 07:38 -0800
        Re: A question on designated initializers Jon Rossen <jonr17@comcast.net> - 2015-11-24 15:54 -0800
          Re: A question on designated initializers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-11-25 01:49 +0100
            Re: A question on designated initializers Don Bruder <dakidd@sonic.net> - 2015-11-24 18:51 -0800
              Re: A question on designated initializers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-11-25 04:41 +0100
                Re: A question on designated initializers Jon Rossen <jonr17@comcast.net> - 2015-11-25 00:25 -0800
                Re: A question on designated initializers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-11-25 14:16 +0100
                Re: A question on designated initializers Jon Rossen <jonr17@comcast.net> - 2015-11-25 17:30 -0800
                Re: A question on designated initializers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-11-26 03:03 +0100
                Re: A question on designated initializers Don Bruder <dakidd@sonic.net> - 2015-11-25 09:08 -0800
                Re: A question on designated initializers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-11-25 18:41 +0100
                Re: A question on designated initializers Don Bruder <dakidd@sonic.net> - 2015-12-02 14:37 -0800
                Re: A question on designated initializers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-12-03 00:58 +0100
          Re: A question on designated initializers Don Bruder <dakidd@sonic.net> - 2015-11-24 18:29 -0800

csiph-web