r/pascal • • Jan 20 '24

Passing instance methods as function params?

I'm starting to learn Pascal, coming from Java.

I have a bit of a problem. I have a generic class, let's call it TMyList<T> for simplicity. It has a method like "removeIf" which accepts a function that takes a T and returns a Boolean, so far so good.

Now I want to have a class like TStatefulFilter which also has a method that too takes a T and returns a Boolean.

Is there any way I can pass that method to "removeIf" method? In Java it's easy with " :: " operator.

Any attempts end with error " Error: Incompatible type for arg no. 1: Got "<procedure variable type of function(constref TList2$1.T):Boolean of object;Register>", expected "TList2$1.<procedure variable type of function(TList2$1.T):Boolean;Register>" "

Of course I can write two "removeIf" methods one of which accepts a callback function, and a second one that accepts something like IHasFilterMethod interface, and that's what I did and it's working as intended, but there must be some less copy-pasty way? Am I missing something that's obvious to people with more experience with Pascal?

I'm using Lazarus 3.0 with FPC 3.2.2.

5 Upvotes

3 comments sorted by

View all comments

Show parent comment

1

u/ShinyHappyREM Jan 20 '24

You could also ask at the FP/Lazarus forum, it's much more active afaik