Who on earth had the idea to make TObject.Create NOT virtual like 20 years or more ago?! :(

Who on earth had the idea to make TObject.Create NOT virtual like 20 years or more ago?! :(

If it were you could create new instances using TClass.

Comments

  1. Well, Delphi's TObject was introduced in 1995, that's almost 20 years ago ;-)

    ReplyDelete
  2. Ok, wasn't sure if Turbo Pascal already had that, but yeah makes sense Delphi being Object Pascal.

    ReplyDelete
  3. Saying 20 years ago now isn't like saying 20 years ago in the last century ;)

    As for Create not being virtual, well, all my persistent classes have a virtual constructor for the purpose of creating them from a TClass (or more precisely from a identifier of some sort tied to the class), so that's not much of an issue in practice.

    And all classes that capture a state may require parameters at construction, an the virtual parameter-less create would be useless/dangerous.

    ReplyDelete
  4. It's not dangerous if you are working with data objects with default constructors but the default constructor does things like creating internal lists or something like this. But using cls.Create where cls is a variable of TClass does not work because that directly calls TObject.Create and not the class I put into cls. Yes, I know I can introduce my own class with virtual constructor but what if I want to use TPersistent as base class for some objects, I have to make TMyObject, TMyPersistent, TMyInterfacedObject and so on, you get the idea.

    Btw, WTF is vmtCreateObject? This looks as if there was a pointer to the constructor inside the class structure but it does not return any useful address when I call it using inline asm.

    ReplyDelete
  5. I think that having parameter-less virtual constructor that constructs the object's internal is a good way to code. For the very same reason Stefan Glienke  had pointed out since we don't want to subclass each and every (TPersistent, TStream, TStringList, TCollection being the most important to me) class so that we just need add Create to.

    Ok we may use RTTI to find and execute the closest (parameter-less) constructor to the class we can but isn't that quite a lot of boiler plate?

    Also having parameter-less constructors makes you (or at least I think) pay more attention to write code with DI in mind (and use a container that can help you with that).

    So yes I feel sad that TObject.Create isn't virtual...

    ReplyDelete
  6. TPersistent should have had a virtual constructor, yes, or they could have made a TVirtualConstructableObject in the low levels, but TObject is a bit too level, and TBH, it's already crowded with baggage.

    The base object class should be leaner and make less assumptions IMHO, and stick to KISS philosophy rather than "Everything + Kitchen Sink".

    ReplyDelete

Post a Comment