Recent

Author Topic: Simple way to inspect complex/array variable on the command line?  (Read 600 times)

rickg

  • Newbie
  • Posts: 2
I'm just starting out with Pascal and prefer working on the command line and fpc with emacs.

"writeln" works fine to inspect scalar variables but I wonder if there is a quick and convenient way to just dump (print, write, or inspect) arrays and complex types (maybe with some sort of implicit conversion) similar to the how the "Debug Inspector" works in the Lazarus IDE?

Alternatively, what is the recommended approach to debugging/inspecting variables without the IDE (with gdb I have to manually format my data types so that's not so convenient)?

simone

  • Sr. Member
  • ****
  • Posts: 253
Re: Simple way to inspect complex/array variable on the command line?
« Reply #1 on: October 20, 2019, 12:22:37 pm »
In order to show a complex data structure (for example an array of string) in a comfortable way, a possible solution is to define a type helper, as in the following code:

Code: Pascal  [Select]
  1. program Project1;
  2. {$mode objfpc}{$H+}
  3. {$MODESWITCH TYPEHELPERS}
  4.  
  5. uses
  6.   Classes,SysUtils;
  7. type
  8.  
  9.   { TStringArrayHelper }
  10.  
  11.   TStringArrayHelper=type helper for TStringArray
  12.     procedure WriteStrArr;
  13.   end;
  14.  
  15. var
  16.   L : TStringArray;
  17.  
  18. { TStringArrayHelper }
  19.  
  20. procedure TStringArrayHelper.WriteStrArr;
  21. var
  22.   S : string;
  23. begin
  24.   for S in Self do
  25.     writeln(S);
  26. end;
  27.  
  28. begin
  29.   L:=TStringArray.Create('aaa','bbb','ccc');
  30.   L.WriteStrArr;
  31.   readln;
  32. end.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 5691
    • wiki
Re: Simple way to inspect complex/array variable on the command line?
« Reply #2 on: October 20, 2019, 01:08:26 pm »
agaik gdb has some support for pretty printer in python? Google gave me https://sourceware.org/gdb/onlinedocs/gdb/Writing-a-Pretty_002dPrinter.html
but I have no idea...

using sources from lazarus trunk, you could look at fixing "fpd" in components/fpdebug/apps
its a commandline to fpdebug.

Before you do to much work, it would be good to discuss what form commands should have.
also I do not know if fpdebugs current output is pretty enough

avk

  • Full Member
  • ***
  • Posts: 141
    • my self-education project
Re: Simple way to inspect complex/array variable on the command line?
« Reply #3 on: October 20, 2019, 01:42:54 pm »
I wonder if there is a quick and convenient way to just dump (print, write, or inspect) arrays and complex types
You might be interested to take a look here: https://github.com/correaelias/TypeUtils

simone

  • Sr. Member
  • ****
  • Posts: 253
Re: Simple way to inspect complex/array variable on the command line?
« Reply #4 on: October 20, 2019, 02:22:14 pm »
You might be interested to take a look here: https://github.com/correaelias/TypeUtils

I didn't know this library and so I proposed to OP a 'home-made' solution. Thanks for the interesting tip.
« Last Edit: October 20, 2019, 02:26:49 pm by simone »

Thaddy

  • Hero Member
  • *****
  • Posts: 9140
Re: Simple way to inspect complex/array variable on the command line?
« Reply #5 on: October 20, 2019, 05:02:34 pm »
That above library is very limited and is not able to do complete reflection (what is what OP implicitly asked). You need (extended) RTTI for that and that is not really finished yet in FPC.
In general, reflection, like in languages as Java/Python/C# will never be really as complete with a true compiled language like Object Pascal or C(++).

At compile time, a good editor like the Lazarus IDE comes a long way,  At run-time, that is another matter.

https://en.wikipedia.org/wiki/Reflection_%28computer_programming%29

That said the question can be solved using RTTI. In Delphi that is already possible (with limitations!) , with FPC in the near future. (trunk is already more complete, but still experimental and not finished)
« Last Edit: October 20, 2019, 08:32:02 pm by Thaddy »
also related to equus asinus.

simone

  • Sr. Member
  • ****
  • Posts: 253
Re: Simple way to inspect complex/array variable on the command line?
« Reply #6 on: October 20, 2019, 07:25:49 pm »
... complete reflection (what is what OP implicitly asked)

If this was the meaning of the OP question, I have misinterpreted and my code is useless.

Thaddy's answer is the correct one.

I only add that reflection is a feature often available in dynamically typed languages, rarely in statically typed ones.
Object Pascal is almost the only one, although its implementation is not complete. Another remarkable
merit of this language.

Thaddy

  • Hero Member
  • *****
  • Posts: 9140
Re: Simple way to inspect complex/array variable on the command line?
« Reply #7 on: October 20, 2019, 08:24:38 pm »
and my code is useless.
Not at all!!! It is very useful and you will be able to expand it later!  :D

Good and useful code. And very good as a well written example.
« Last Edit: October 20, 2019, 08:29:57 pm by Thaddy »
also related to equus asinus.

simone

  • Sr. Member
  • ****
  • Posts: 253
Re: Simple way to inspect complex/array variable on the command line?
« Reply #8 on: October 20, 2019, 08:34:12 pm »
I'm very pleased to read these words. My vanity could increase dramatically!  :)

rickg

  • Newbie
  • Posts: 2
Re: Simple way to inspect complex/array variable on the command line?
« Reply #9 on: October 21, 2019, 04:10:51 am »
Thank you all for all the help, this is super helpful. "TypeUtils" works brilliantly.