Recent

Author Topic: Writing complex data to console automatically  (Read 3829 times)

correa.elias

  • New Member
  • *
  • Posts: 18
Writing complex data to console automatically
« on: October 04, 2018, 11:39:07 pm »
Hello all;

Theres any procedure in freepascal that can print (Write/WriteLn) the content of more complex data (at least arrays and records) automatically for debugging/logging porpouses?

for example:

Code: Pascal  [Select][+][-]
  1. type
  2.   TVertex = record
  3.     FX, FY, FZ: Single;
  4.   end;
  5. var
  6.   Vertex: TVertex;
  7. begin
  8.   with Vertex do
  9.   begin
  10.     FX := 1.0;
  11.     FY := 2.0;
  12.     FZ := 3.0;
  13.   end;
  14.   ComplexWriteLn(Vertex); //this dont exists, but should print something like {1.0, 2.0, 3.0} to the console.
  15. end.
  16.  

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Writing complex data to console automatically
« Reply #1 on: October 05, 2018, 12:27:02 am »
I take this as home work so I'll try to control myself..
 
 You need to create a Procedure or Function of that name and of that parameter type...

 The procedure needs to appear in your code before the Begin … End so the compiler
will know about it.

 Procedure ComplexWriteln( SomeData:TVertex)

I gave you the start of your home work...  :D

The only true wisdom is knowing you know nothing

correa.elias

  • New Member
  • *
  • Posts: 18
Re: Writing complex data to console automatically
« Reply #2 on: October 05, 2018, 01:25:47 am »
Sorry, english is not my native language so i think i was not clear... I Just want to know if theres some kind of advanced WriteLn in RTL that is capable of printing any kind of data, including any records and arrays in the same vein as 'console.log' from javascript or 'print' in python language.

Im asking this because im looking into run time type information(rtti, typinfo unit) and believe its just doable (and usefull)...
« Last Edit: October 05, 2018, 01:27:44 am by correa.elias »

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Writing complex data to console automatically
« Reply #3 on: October 05, 2018, 02:30:49 am »
fpc is a compiler and as such much of the naming of items are gone, in other words they are known
only at compile time..

 However, there is some light at the end of the tunnel. You can use Advanced Records and include
method that will generate a string output for you..

  Write a method inside the Record that will produce a string for you..
so you can then later on do this..

 WriteLn(Vertex.AsString);

 If you need help with that I'll come back.

Btw.
  There is the VARIANTS you can use where as this type can contain many different types
and be past to a single function of your making, from there you generate the output.

« Last Edit: October 05, 2018, 02:50:19 am by jamie »
The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Writing complex data to console automatically
« Reply #4 on: October 05, 2018, 07:26:33 am »
There are several ways to do this, easiest is:
Code: Pascal  [Select][+][-]
  1. procedure ComplexWriteln(const v:Tvertex);
  2. // using write/writeln formatting
  3. begin
  4.   writeln('{',v.FX:2:2,',',v.FY:2:2,',',v.FZ:2:2,'}');
  5. end;
  6.  
  7. procedure ComplexWriteln2(const v:Tvertex);
  8. // format from sysutils
  9. begin
  10.   writeln(format('{%2.2f, %2.2f, %2.2f}',[v.FX,v.FY,v.FZ]));
  11. end;

Specialize a type, not a var.

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Writing complex data to console automatically
« Reply #5 on: October 05, 2018, 02:24:51 pm »
fpc is a compiler and as such much of the naming of items are gone, in other words they are known
only at compile time..
As long as the record type is known you can do magic at runtime using RTTI, but I considered that too advanced. (unless we do it <grin>)
It is also possible to let those be handled by e.g. Python itself. There are more options. E.g. the type helpers from sysutils.
Combining generics + type helpers would go a  long way to achieve this.
« Last Edit: October 05, 2018, 02:32:44 pm by Thaddy »
Specialize a type, not a var.

correa.elias

  • New Member
  • *
  • Posts: 18
Re: Writing complex data to console automatically
« Reply #6 on: October 05, 2018, 04:20:50 pm »
I know its possible to use type helpers, advanced records and generics, but the key here is automatically (i mean, i dont want to write a ToString method for every record or array) this leaves only RTTI in the table. Will try to write a proof of concept using it. Thanks for your answers :).
« Last Edit: October 05, 2018, 04:24:49 pm by correa.elias »

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Writing complex data to console automatically
« Reply #7 on: October 05, 2018, 05:09:08 pm »
I know its possible to use type helpers, advanced records and generics, but the key here is automatically (i mean, i dont want to write a ToString method for every record or array) this leaves only RTTI in the table. Will try to write a proof of concept using it. Thanks for your answers :).
Automatically also means there is a similar function - in this case - written in another language - C in this case - that makes that possible in Python. (Python is rather helpless without its libraries)
What we can do is write a unit that you can include in your own program that does something similar. But it is a good exercise to do that yourself.
Specialize a type, not a var.

correa.elias

  • New Member
  • *
  • Posts: 18
Re: Writing complex data to console automatically
« Reply #8 on: October 11, 2018, 04:39:41 pm »
Hello, i implemented it here:

https://github.com/correaelias/TypeUtils
« Last Edit: October 11, 2018, 04:43:30 pm by correa.elias »

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Writing complex data to console automatically
« Reply #9 on: October 11, 2018, 05:01:31 pm »
That's the way to do it! That's the spirit!

I will review your code A.S.A.P.

Thumbs Up!!  :) :)
Specialize a type, not a var.

correa.elias

  • New Member
  • *
  • Posts: 18
Re: Writing complex data to console automatically
« Reply #10 on: October 11, 2018, 06:30:48 pm »
Thanks Thaddy. Your review is welcome.

 

TinyPortal © 2005-2018