That's why the code is not as simple (and it does some extra things during compilation which is not possible for "normal" functions). So creating a similar function isn't that easy (if at all possible).
Trouble is, as things are at the moment, there's no way (for a mere mortal such as I) to even attempt to study what is possible let alone emulate it.
This bothers me in more ways than one. We have an open source compiler; yet, some /code/ is not available because it's obscured out of human eyes.
You say you don't want to "capture" the writeln() output? What DO you want?? Because what you suggested, does just that.
Say, we had a function with all (or as much/many as possible) features and frills of WriteLn() but one that returned a string. Lets call it WriteLnAsString().
I could use it in countless places. I could directly output stuff to TStream, EditBox, Memo etc. etc.
A very simple assignment.
With redirection, I have to go through unnecessary hoops.
Another option would be to create your own writeln(). But it can have only defined (in number and type) parameters.
I did consider that. But, it is very laborious and error-prone process. You have to go through the source you're porting and catch every single variation and provide for them.
Add to that the fact that there's no documentation (that I could find) about every feature of WriteLn, you're in complete dark. 'Source is the documentation' falls flat here since there is no single source for WriteLn.
Actually, this is not the first time I needed 'WriteLnAsString()'. A couple years ago I met the same wall, and I gave up (it was a more complex code).
Incidentally, emulating WriteLn using string concatenation is not that easy. WriteLn does a lot more than that (internally formatting numbers, adding tabs, etc.).
writestr(S, 'hello', ' ', 'world', 2018);
// now do something with S
Yeah, no Delphi compatibility.
I suppose I could create a DLL using FPC and use it with Delphi. Horror.
Just because there's no standalone function.
I could, if I had the temerity to put myself through that torture, do something like WriteLn(output, [arguments]) and then read it right after..
Definitely not an elegant solution.
Also note that the original HeadConv is really old and should have a GUI version already. There is also a newer version in JEDI of it.
I have started with the Jedi version. But, it didn't compile with anything later than D7.
While its code is hard enough already, the fact that it uses countless local functions/procedures and dozens of global variables doesn't make it any easier to follow.
I have turned it into a class with all those local functions/procedures and global variables moved into member of private section.
IOW, I have spent sometime on it and it works.
The only remaining issue is to get rid of all those System.Text stuff. Doing that will make it easier to understand and then improve.
Yet, WriteLn is blocking me --or, bothering me-- as the current proposals will only add further complexity to the code.
I have to say, I have come to hate WriteLn with an immesurable passion...
I have never used it. But the danm thing comes up in legacy code.