Watch, Follow, &
Connect with Us

Please visit our new home
community.embarcadero.com.


Welcome, Guest
Guest Settings
Help

Thread: class function type doesn't seem possible


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 11 - Last Post: Sep 26, 2017 6:25 AM Last Post By: Rudy Velthuis (...
Markus Humm

Posts: 24
Registered: 12/17/09
class function type doesn't seem possible  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 19, 2017 7:49 AM
Hello,

using Delphi 10.1 Berlin I'm trying to declare a class function type:

e.g.

type
TMyType = class function(b: Boolean): Integer;

But the compiler doesn't like this.
Can somebody please explain to me why this is the case?

Greetings

Markus
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: class function type doesn't seem possible  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 19, 2017 9:14 AM   in response to: Markus Humm in response to: Markus Humm
Markus Humm wrote:

using Delphi 10.1 Berlin I'm trying to declare a class function type:

e.g.

type
TMyType = class function(b: Boolean): Integer;

But the compiler doesn't like this.

Can somebody please explain to me why this is the case?

Because it is not valid.

There are only two kinds of function pointers: plain C-style function
pointers, and object method pointers (I won't get into anonymous
functions and function references). There is no such thing as a class
function pointer.

A 'class' function that is not declared as 'static' has a hidden Self
parameter, which points to the class type rather than a particular
object instance. In order to call a 'class' function via a pointer,
the compiler needs to know that it has to supply a value for that
hidden parameter. To do that, use this declaration syntax instead:

type
  TMyType = function(b: Boolean): Integer of object;


On the other hand, if the 'class' function is declared as 'static',
there is no hidden Self parameter, so the function is compatible with a
C-style function pointer instead:

type
  TMyType = function(b: Boolean): Integer;


--
Remy Lebeau (TeamB)
Markus Humm

Posts: 5,113
Registered: 11/9/03
Re: class function type doesn't seem possible  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 19, 2017 9:24 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Am 19.09.2017 um 18:14 schrieb Remy Lebeau (TeamB):
[snip]


A 'class' function that is not declared as 'static' has a hidden Self
parameter, which points to the class type rather than a particular
object instance. In order to call a 'class' function via a pointer,
the compiler needs to know that it has to supply a value for that
hidden parameter. To do that, use this declaration syntax instead:

type
  TMyType = function(b: Boolean): Integer of object;


On the other hand, if the 'class' function is declared as 'static',
there is no hidden Self parameter, so the function is compatible with a
C-style function pointer instead:

type
  TMyType = function(b: Boolean): Integer;


Hello,

thanks for the explanation in the first place.
I don't have much time right now and no compiler to test, but if I
declare a

var
list: TList<string, TMyType>;

and TMyType is

type
TMyType = function(b: Boolean): Integer of object;

and I have a class:

TMyClass = class(TObject)
public
class function MyClassFunc(b: Boolean): Integer;
end;

Can I later on add that to the list like this?

list.Add('MyName', TMyClass.MyClassFunc);

Greetings

Markus
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: class function type doesn't seem possible  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 19, 2017 12:05 PM   in response to: Markus Humm in response to: Markus Humm
Markus Humm wrote:

var
list: TList<string, TMyType>;

The Generic version of TList only accepts 1 type parameter. You should
use TDictionary instead:

http://docwiki.embarcadero.com/Libraries/en/System.Generics.Collections.TDictionary

var
  list: TDictionary<string, TMyType>;


Can I later on add that to the list like this?

list.Add('MyName', TMyClass.MyClassFunc);

Yes. And then you can call it like this:

var
  Num: Integer;
 
Num := list['MyName'](True);


--
Remy Lebeau (TeamB)
Paul TOTH

Posts: 196
Registered: 1/2/17
Re: class function type doesn't seem possible  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 20, 2017 12:10 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Le 19/09/2017 à 21:05, Remy Lebeau (TeamB) a écrit :
Markus Humm wrote:

var
list: TList<string, TMyType>;

The Generic version of TList only accepts 1 type parameter. You should
use TDictionary instead:

http://docwiki.embarcadero.com/Libraries/en/System.Generics.Collections.TDictionary

var
   list: TDictionary<string, TMyType>;


Can I later on add that to the list like this?

list.Add('MyName', TMyClass.MyClassFunc);

Yes. And then you can call it like this:

I fact it depends of Delphi version

I still have a XE3 project that do not support that (E2009)

var
   Num: Integer;
 
Num := list['MyName'](True);

Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: class function type doesn't seem possible  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 20, 2017 9:31 AM   in response to: Paul TOTH in response to: Paul TOTH
Paul TOTH wrote:

I still have a XE3 project that do not support that (E2009)

Support what? Generics? XE3 supports Generics (they were introduced
in Delphi 2009), and it has TDictionary (in the
'System.Generics.Collections' unit, same as the Generic TList).

--
Remy Lebeau (TeamB)
Rudy Velthuis (...


Posts: 7,731
Registered: 9/22/99
Re: class function type doesn't seem possible  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 22, 2017 1:44 AM   in response to: Paul TOTH in response to: Paul TOTH
Paul TOTH wrote:

Le 19/09/2017 à 21:05, Remy Lebeau (TeamB) a écrit :
Markus Humm wrote:

var
list: TList<string, TMyType>;

The Generic version of TList only accepts 1 type parameter. You
should use TDictionary instead:

http://docwiki.embarcadero.com/Libraries/en/System.Generics.Collections.TDictionary

var
   list: TDictionary<string, TMyType>;


Can I later on add that to the list like this?

list.Add('MyName', TMyClass.MyClassFunc);

Yes. And then you can call it like this:

I fact it depends of Delphi version

I still have a XE3 project that do not support that (E2009)

XE3 most definitely supports generics, and TDictionary<T>.

But E2009 is "Incompatible types". That means the compiler has detected
a difference between the declaration and the use of a procedure.

--
Rudy Velthuis http://www.rvelthuis.de

"Either he's dead or my watch has stopped."
-- Groucho Marx
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: class function type doesn't seem possible  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 22, 2017 5:20 PM   in response to: Rudy Velthuis (... in response to: Rudy Velthuis (...
Rudy Velthuis (TeamB, MVP) wrote:

XE3 most definitely supports generics, and TDictionary<T>.

But E2009 is "Incompatible types". That means the compiler has
detected a difference between the declaration and the use of a
procedure.

Then you are ding something wrong with it. Please show the actual code.

--
Remy Lebeau (TeamB)
Paul TOTH

Posts: 196
Registered: 1/2/17
Re: class function type doesn't seem possible  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 23, 2017 1:01 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Le 23/09/2017 à 02:20, Remy Lebeau (TeamB) a écrit :
Rudy Velthuis (TeamB, MVP) wrote:

XE3 most definitely supports generics, and TDictionary<T>.

But E2009 is "Incompatible types". That means the compiler has
detected a difference between the declaration and the use of a
procedure.

Then you are ding something wrong with it. Please show the actual code.

well it' not about Generic, it's about class function and function
types...I've write something under Berlin that do not compiles under XE3

I've put sorting function in the class to call
MyList.Sort(TItem.SortByName) for instance...but don't remember the
case...and can't reproduice it
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: class function type doesn't seem possible  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 25, 2017 11:44 AM   in response to: Paul TOTH in response to: Paul TOTH
Paul TOTH wrote:

well it' not about Generic, it's about class function and function
types...I've write something under Berlin that do not compiles under
XE3

Without seeing an actual code example, nobody can tell you what is
wrong with it, or whether it is supposed to work in older versions or
not.

I've put sorting function in the class to call
MyList.Sort(TItem.SortByName) for instance...but don't remember the
case...and can't reproduice it

TList.Sort() expects a standalone function, not a class method.

TList<T>.Sort() expects an object that implements the IComparer<T>
interface.

--
Remy Lebeau (TeamB)
Rudy Velthuis (...


Posts: 7,731
Registered: 9/22/99
Re: class function type doesn't seem possible  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 26, 2017 6:25 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:

TList<T>.Sort() expects an object that implements the IComparer<T>
interface.

Which can be obtained from TComparer<T>.Default or
TComparer<T>.Construct.

--
Rudy Velthuis http://www.rvelthuis.de

"Conservatives are not necessarily stupid, but most stupid
people are conservatives."
-- John Stuart Mill
Rudy Velthuis (...


Posts: 7,731
Registered: 9/22/99
Re: class function type doesn't seem possible  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 24, 2017 11:24 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:

Rudy Velthuis (TeamB, MVP) wrote:

XE3 most definitely supports generics, and TDictionary<T>.

But E2009 is "Incompatible types". That means the compiler has
detected a difference between the declaration and the use of a
procedure.

Then you are ding something wrong with it. Please show the actual
code.

I guess you mean the OP. I am not doing anything wrong. <g>

--
Rudy Velthuis http://www.rvelthuis.de

Brook's Law: Adding manpower to a late software project makes it
later.
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02