Re: [fpc-pascal] Caller agnostic procedure variables

2022-02-14 Thread Ryan Joseph via fpc-pascal


> On Feb 15, 2022, at 2:09 PM, Michael Van Canneyt via fpc-pascal 
>  wrote:
> 
> I've answered this question before:
> 
> The "Reference to procedure" that will be part of anonymous functionswill do 
> this for you.

I'm sorry I forgot! This thing keeps coming up for me and driving me nuts but I 
knew I asked in passing before.

So "reference to procedure" are going to be the new standard? To extend my 
example it would look like this? I please remind me, is there a closure 
involved which is capturing state and adding overhead?

I'm curious what this type actually is also, maybe a dispatch table which wraps 
the existing types or is it something totally new?



type
  TMyAction = reference to procedure;

procedure DoThis(action: TMyAction);
  begin
action();
  end;

Regards,
Ryan Joseph

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Caller agnostic procedure variables

2022-02-14 Thread Michael Van Canneyt via fpc-pascal



On Tue, 15 Feb 2022, Ryan Joseph via fpc-pascal wrote:


This has been a constant problem for me with FPC and wanted to make a formal 
post with code examples since I've only mentioned it in passing before.

How can it be achieved to have a caller agnostic procedure variables? I've tried 
making some big crazy dispatch record that uses generics but because generics don't 
support variable templates (like some languages have TClass) it was 
limited and clunky to use.

The problem is that from the perspective of the receiver it shouldn't really care what 
the caller has provided except for that there is a procedure that needs to be called. For 
example if there is a "sort" function that takes a procedure variable it 
shouldn't care if the procedure is a global function, a method or a nested function (and 
eventually a closure).

It feels like the compiler needs a new type which encapsulates these different 
types but I'm not sure how this all works internally. Any thoughts on this?


I've answered this question before:

The "Reference to procedure" that will be part of anonymous functionswill do 
this for you.

Michael.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


[fpc-pascal] Caller agnostic procedure variables

2022-02-14 Thread Ryan Joseph via fpc-pascal
This has been a constant problem for me with FPC and wanted to make a formal 
post with code examples since I've only mentioned it in passing before.

How can it be achieved to have a caller agnostic procedure variables? I've 
tried making some big crazy dispatch record that uses generics but because 
generics don't support variable templates (like some languages have 
TClass) it was limited and clunky to use.

The problem is that from the perspective of the receiver it shouldn't really 
care what the caller has provided except for that there is a procedure that 
needs to be called. For example if there is a "sort" function that takes a 
procedure variable it shouldn't care if the procedure is a global function, a 
method or a nested function (and eventually a closure).

It feels like the compiler needs a new type which encapsulates these different 
types but I'm not sure how this all works internally. Any thoughts on this?

===

{$mode objfpc}

program procvars;

type
  TMyAction = procedure;
  TMyClass = class
procedure MyAction;
  end;

procedure DoThis(action: TMyAction);
  begin
action();
  end;

procedure MyAction;
  begin
  end;

procedure Test;
  
  procedure MyNestedAction; 
  begin
  end;

  var
c: TMyClass;
  begin
// of object
c := TMyClass.Create;
DoThis(@c.MyAction); // error: Incompatible type for arg no. 1: Got 
"", expected 
""

// is nested
DoThis(@MyNestedAction); // error: Incompatible type for arg no. 1: Got 
"", expected 
""

// normal
DoThis(@MyAction);
  end;

begin

end.

Regards,
Ryan Joseph

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Sorting a Stringlist on ValueFromIndex

2022-02-14 Thread Michael Van Canneyt via fpc-pascal



On Mon, 14 Feb 2022, James Richters via fpc-pascal wrote:


Is there a quick way to sort a stringlist on it's   .ValueFromIndex field
instead of it's   .Names field?



// Custom compare function
function CompareValues(List: TStringList; Index1, Index2: Integer): Integer;

begin
  Result:=CompareText(List.ValueFromIndex[Index1],List.ValueFromIndex[Index2])
end;

// Sort using custom compare function
Mylist.CustomSort(@CompareValues);

Michael.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


[fpc-pascal] Sorting a Stringlist on ValueFromIndex

2022-02-14 Thread James Richters via fpc-pascal
Is there a quick way to sort a stringlist on it's   .ValueFromIndex field
instead of it's   .Names field?
 
James
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] bug or feature?

2022-02-14 Thread Elmar Haneke via fpc-pascal



This can't be right, can it?

type
   TBird = class
 procedure Fly;
   end;
   TEagle = TBird; // alias

procedure TEagle.Fly;
begin
end;



It does depend on what you expect an Type-Alias to be.

If you expect the compiler to generate an compatible class then your 
example must not be compilable.


If you expect an alias die make the Typenames "TEagle" and "TBird" 
equivalent it is no problem for the compiler which name you use at what 
place.


In any case I would judge such code as "bad style" - programmers should 
avoid doing so.



Elmar


___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal