Re: [fpc-pascal] Iterating over elements from GetDynArrayProp

2022-10-15 Thread Michael Van Canneyt via fpc-pascal



On Sat, 15 Oct 2022, Hairy Pixels via fpc-pascal wrote:


How can I iterate over the pointer that is returned from GetDynArrayProp? I 
tried to cast it as a dynamic array of the element type but calling Length() on 
the returned value gave garbled data.

For example with a property of the type “array of TObject":

type
 TObjectArray = array of TObject;
 PObjectArray = ^TObjectArray;
var
 ObjectArray: PObjectArray;
begin
 ObjectArray := PObjectArray(GetDynArrayProp(AObject, PropertyInfo));


as far as I kbow, this should be
 ObjectArray := TObjectArray(GetDynArrayProp(AObject, PropertyInfo));
i.e. you get the actual array as a result.

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


Re: [fpc-pascal] Iterating over elements from GetDynArrayProp

2022-10-15 Thread Hairy Pixels via fpc-pascal


> On Oct 15, 2022, at 3:03 PM, Michael Van Canneyt  
> wrote:
> 
> as far as I kbow, this should be
> ObjectArray := TObjectArray(GetDynArrayProp(AObject, PropertyInfo));
> i.e. you get the actual array as a result.
> 

You’re right, I didn’t need that pointer cast. I’m still curious though why 
that didn’t work. Shouldn’t the pointer still just point to the dynamic array 
address and basically do nothing?

Regards,
Ryan Joseph

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


Re: [fpc-pascal] Iterating over elements from GetDynArrayProp

2022-10-15 Thread Michael Van Canneyt via fpc-pascal



On Sat, 15 Oct 2022, Hairy Pixels wrote:





On Oct 15, 2022, at 3:03 PM, Michael Van Canneyt  wrote:

as far as I kbow, this should be
ObjectArray := TObjectArray(GetDynArrayProp(AObject, PropertyInfo));
i.e. you get the actual array as a result.



You’re right, I didn’t need that pointer cast. I’m still curious though why 
that didn’t work. Shouldn’t the pointer still just point to the dynamic array 
address and basically do nothing?


No, it's perfectly logical what is happening. 
You can't cast a type (TA) to a pointer to that type (^PTA) and expect the compiler to automagically forget the

extra dereference when you dereference it...

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


[fpc-pascal] Open array compatibility

2022-10-15 Thread Hairy Pixels via fpc-pascal
Another question I just ran in to. Why isn’t "array of TMyClass” compatible 
with “array of TObject” open array? TMyClass is a descendant of TObject and 
dynamic arrays are compatible with open arrays right?

==

type
  TMyClass = class
  end;

procedure MyProcedure(values: array of TObject); 
begin
end;

var
  myArray: array of TMyClass;
begin
  MyProcedure(myArray); // error: Incompatible type for arg no. 1: Got 
"{Dynamic} Array Of TMyClass", expected "{Open} Array Of TObject"

Regards,
Ryan Joseph

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


Re: [fpc-pascal] Open array compatibility

2022-10-15 Thread Michael Van Canneyt via fpc-pascal



On Sat, 15 Oct 2022, Hairy Pixels via fpc-pascal wrote:


Another question I just ran in to. Why isn’t "array of TMyClass” compatible 
with “array of TObject” open array? TMyClass is a descendant of TObject and dynamic 
arrays are compatible with open arrays right?

==

type
 TMyClass = class
 end;

procedure MyProcedure(values: array of TObject); 
begin

end;

var
 myArray: array of TMyClass;
begin
 MyProcedure(myArray); // error: Incompatible type for arg no. 1: Got "{Dynamic} Array Of 
TMyClass", expected "{Open} Array Of TObject"


Rougly said:
Arrays are considered equal if the element type is equal.
And equal does not mean 'inherits from'.

One reason we do not allow descendents is that if you have a var array, the
returned value may contain classes of a type that the caller does not expect.
(This is the same reason why (var value: TObject) requires an exact match in 
type)

But a case could be made to allow it in case the parameter is passed by value.

You can force it with a typecast:

Type
  TObjectArray = Array of Object;

var
  myArray: array of TMyClass;

begin
  MyProcedure(TObjectArray(myArray)); 
end.


But then you're on you own, as you are deliberately bypassing the compiler's
typechecking (as is the case with all typecasts...).

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


Re: [fpc-pascal] Open array compatibility

2022-10-15 Thread Hairy Pixels via fpc-pascal


> On Oct 15, 2022, at 3:58 PM, Michael Van Canneyt  
> wrote:
> 
> One reason we do not allow descendents is that if you have a var array, the
> returned value may contain classes of a type that the caller does not expect.
> (This is the same reason why (var value: TObject) requires an exact match in 
> type)
> 

Ah ok, I guess there would need to be an “array of class” like it used with 
generic param constraints i.e. . I’m trying to do some tricks with 
overloading which is why I ask.

Regards,
Ryan Joseph

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


[fpc-pascal] Exploring block statements with CodeTools not working

2022-10-15 Thread Hairy Pixels via fpc-pascal
I’m trying to look at the code in a the begin..end section of this sample 
program but for some reason when it gets to the BeginBlock node child count is 
always 0 (other nodes are as expected though). I’m calling Explore correctly I 
think so what could the problem be?

Here is the little test program I’m using along with a snippet which explores 
the code and walks the tree.

Btw, I understand this question is basically for Mattias but I don’t know where 
else to post. Does Mattias prefer I post in one of these forums 
https://forum.lazarus.freepascal.org or maybe email privately with code tools 
questions?



  program test;

  var
A: Integer;
  begin
A := 1;
  end.



CodeToolBoss.Explore(Code, Tool, true);

Node := tool.Tree.Root;
while Node <> nil do
  begin
writeln(Node.DescAsString, ‘ -> ’, Node.ChildCount);
Node := Node.Next;
  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] Exploring block statements with CodeTools not working

2022-10-15 Thread Ondrej Pokorny via fpc-pascal

Am 16.10.2022 um 04:19 schrieb Hairy Pixels via fpc-pascal:

Btw, I understand this question is basically for Mattias but I don’t know where 
else to post. Does Mattias prefer I post in one of these forums 
https://forum.lazarus.freepascal.org or maybe email privately with code tools 
questions?


Though I am not Mattias and cannot comment on his preferences:
CodeTools are part of the Lazarus project, hence the question belongs to 
the Lazarus mailing list https://lists.lazarus-ide.org/listinfo/lazarus 
or one of the IDE categories in the forum.


Ondrej

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