Recent

Author Topic: Status of the keywords abstract and sealed in the context of plain old object  (Read 487 times)

avk

  • Sr. Member
  • ****
  • Posts: 407
    • my self-education project
I did not find any mention in the documentation that these keywords are supported in the context of object.
Nevertheless, this code is compiled:
Code: Pascal  [Select][+][-]
  1. type
  2.   TObj = object abstract
  3.   end;
  4.  
  5.   TMyObj = object(TObj)
  6.   end;
  7.  
but this one does not:
Code: Pascal  [Select][+][-]
  1. type
  2.   TObj= object sealed
  3.   end;
  4.  
  5.   TMyObj = object(TObj)
  6.   end;  
  7.  
So what is the current status really?

PascalDragon

  • Hero Member
  • *****
  • Posts: 2892
  • Compiler Developer
You are aware that the purpose of sealed is that the second one does not compile?

avk

  • Sr. Member
  • ****
  • Posts: 407
    • my self-education project
Hmm, maybe a language barrier?

The documentation is silent that these keywords can be applied to object.

Kays

  • Sr. Member
  • ****
  • Posts: 299
  • Whasup!?
    • KaiBurghardt.de
I did not find any mention in the documentation that these keywords are supported in the context of object. […]
Good point. Currently, they’re only mentioned in the context of class.

As long as an object doesn’t contain virtual methods it is virtually a more sophisticated record.

The abstract keyword is probably, like it is documented for class, recognized for “Delphi compatibility”, but doesn’t have any other impact. If I search for oo_is_abstract it primarily appears in parse_object_options. Instantiating an abstract class will merely lead to a warning (type_w_instance_abstract_class) produced in pexpr.pas, but that wouldn’t really make sense for objects, since they’re always already allocated on the stack.

As you’ve already noticed, sealed already works for object and emits parser_e_sealed_descendant. This message says “Class” though, so it can be confusing.

I’ve reported a bug about the missing documentation: issue #38535.
« Last Edit: February 23, 2021, 02:41:43 pm by Kays »
Yours Sincerely
Kai Burghardt

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 9220
  • FPC developer.
Does TP object have abstract methods at all?

avk

  • Sr. Member
  • ****
  • Posts: 407
    • my self-education project
It seems that the hedgehog is too lazy bird, until you kick it, will not fly?

lucamar

  • Hero Member
  • *****
  • Posts: 3771
Does TP object have abstract methods at all?

Yes, they do ... in FPC.

But if you mean real TP-compilable objects then no, they could only be static, virtual or dynamic. What I don't know is whether FPC in TP-mode allows them (abstract) or not.
« Last Edit: February 23, 2021, 08:14:45 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

PascalDragon

  • Hero Member
  • *****
  • Posts: 2892
  • Compiler Developer
Hmm, maybe a language barrier?

The documentation is silent that these keywords can be applied to object.

Yes, the documentation is silent about it, however you also wrote "Nevertheless, this code is compiled: <example> but this one does not <example>". That the second one does not compile is the purpose of the sealed directive.
The first one would only trigger a warning if the object is instantiated using a New with a constructor, but currently no warning is generated, so that should probably be fixed...

@Kays: yes, it's a warning for Delphi compatibility, but one can easily escalate it to error using {$warn constructing_abstract error} (this will also turn the warning for abstract methods into an error).

avk

  • Sr. Member
  • ****
  • Posts: 407
    • my self-education project
Yes, I gave these two examples just to demonstrate that it works, despite the lack of documentation.

 

TinyPortal © 2005-2018