Recent

Author Topic: Calling Super in Objective-C  (Read 412 times)

syntonica

  • Full Member
  • ***
  • Posts: 116
Calling Super in Objective-C
« on: December 19, 2019, 03:21:16 am »
Here's what I got:
Code: Pascal  [Select]
  1. {$mode objfpc}
  2. {$modeswitch objectivec1}
  3.  
  4. procedure MyNSViewSubclass.keyDown(theEvent: NSEvent);
  5. var
  6.   Consumed: Boolean;
  7.  
  8. begin
  9.   { do some stuff, but we aren't going to consume the event }
  10.   if not Consumed then super.keyDown(theEvent);  // [super keyDown:theEvent]
  11. end;
  12.  

How do I effect the call to super to pass the event along?  I've tried many variations, searched in all the usual places, no luck... :(

syntonica

  • Full Member
  • ***
  • Posts: 116
Re: Calling Super in Objective-C
« Reply #1 on: December 19, 2019, 08:01:51 am »
Finally found it!
Code: Pascal  [Select]
  1. if not Consumed then Inherited keyDown(theEvent);
  2.  
Not exactly an obvious construct.   :-[

Thaddy

  • Hero Member
  • *****
  • Posts: 9424
Re: Calling Super in Objective-C
« Reply #2 on: December 19, 2019, 08:21:05 am »
Why? Technically inherited and super are similar, just differently named in the different languages.
Both mean to allow you to use the implementation of the current class its  ancestor.
I wrote similar, because there are some differences: e.g. in Objective C, you can dispatch messages to the super class(es) directly, using objc_msgSendSuper (xxx) and is static, whereas Object Pascal class dispatching is dynamic through its VMT.
« Last Edit: December 19, 2019, 08:36:45 am by Thaddy »
also related to equus asinus.

PascalDragon

  • Hero Member
  • *****
  • Posts: 894
  • Compiler Developer
Re: Calling Super in Objective-C
« Reply #3 on: December 19, 2019, 09:21:35 am »
Finally found it!
Code: Pascal  [Select]
  1. if not Consumed then Inherited keyDown(theEvent);
  2.  
Not exactly an obvious construct.   :-[
It is in Object Pascal. Here, it's documented. It isn't that far-fetched that Objective Pascal uses the same way to call a parent's method.

syntonica

  • Full Member
  • ***
  • Posts: 116
Re: Calling Super in Objective-C
« Reply #4 on: December 19, 2019, 09:26:00 am »
Why?
Because I just spent spent the day translating [bracketed things] into dotOperator.things and "Inherited" doesn't follow the pattern. Only reason I found it is that I was looking at the Java->Pascal page.

syntonica

  • Full Member
  • ***
  • Posts: 116
Re: Calling Super in Objective-C
« Reply #5 on: December 19, 2019, 09:33:20 am »
Finally found it!
Code: Pascal  [Select]
  1. if not Consumed then Inherited keyDown(theEvent);
  2.  
Not exactly an obvious construct.   :-[
It is in Object Pascal. Here, it's documented. It isn't that far-fetched that Objective Pascal uses the same way to call a parent's method.
I was assuming there was a special construct for this.  Now there will be a Google entry forever associating 'super' with 'Inherited' so other poor souls following in my footsteps don't get as lost.  :D

Anyway, I'm finally getting close to a clean compile, although there are a couple of ugly bumps in the road ahead due to some changes I made deep in the bowels of my engine.  Fortunately, FPC never complains about how many times I invoke it. Just my bad code.