Skip to content

Statement not true #18

@alpinistbg

Description

@alpinistbg

In 8.2 Callbacks (aka events, aka pointers to functions, aka procedural variables)
at the end of page 70 (in .pdf):

Unfortunately, you need to write ugly @TMyClass(nil).Add instead of just @TMyClass.Add.

That's not true.

It didn't occur to me to use class methods for procedural variable of object namely because I know there is no hidden Self in it (procedure of object means it is a <object_reference, method_address> pair).

To my surprise, your example works! But IMO it shouldn't. May be it works because one can invoke a class method using an instance only to specify indirectly it's class and the actual reference is just ignored?

Back to the statement:

Unfortunately, you need to write ugly @TMyClass(nil).Add instead of just @TMyClass.Add.

I've tried:

  M := @TMyClass.Add;

And it works (thus your statement is not true).

Only when Add is a normal method, not a class one, then it won't work and it must be written as @TMyClass(nil).Add, i.e. a reference must be specified.

I'm still confused that it is possible to assign Self-less class method to a procedural variable of object.

Lazarus 1.9.0 r64660M FPC 3.1.1 x86_64-linux-gtk2

Edit: Corrections. There is a Self pointer in class methods according to: https://www.freepascal.org/docs-html/ref/refsu28.html and it points to the VMT. But what I've tried still works.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions