Recent

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

avk

  • Hero Member
  • *****
  • Posts: 752
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: 5486
  • Compiler Developer
You are aware that the purpose of sealed is that the second one does not compile?

avk

  • Hero Member
  • *****
  • Posts: 752
Hmm, maybe a language barrier?

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

Kays

  • Hero Member
  • *****
  • Posts: 578
  • 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

  • Administrator
  • Hero Member
  • *
  • Posts: 11459
  • FPC developer.
Does TP object have abstract methods at all?

avk

  • Hero Member
  • *****
  • Posts: 752
It seems that the hedgehog is too lazy bird, until you kick it, will not fly?

lucamar

  • Hero Member
  • *****
  • Posts: 4219
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: 5486
  • 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

  • Hero Member
  • *****
  • Posts: 752
Yes, I gave these two examples just to demonstrate that it works, despite the lack of documentation.

 

TinyPortal © 2005-2018