[v2] toStr() – represent anything as a string (for debugging, etc.)

Post your working scripts, libraries and tools.
User avatar
SALZKARTOFFEEEL
Posts: 89
Joined: 10 May 2017, 11:14
Location: Germany
Contact:

[v2] toStr() – represent anything as a string (for debugging, etc.)

28 Mar 2019, 10:18

Hi Everyone!

I was inspired to do this project, because I've been working a lot with JavaScript lately, which (in modern browsers like Chrome) has an excellent way of representing any type of object or other value in the console.
In AHK, when you try to display any object whatsoever, you just get a blank string.
This is obviously quite frustrating and not a good solution.
While implementing such a feature in AHK itself would probably be a better solution, I think my toStr() function serves its purpose quite well!

How It Works

It couldn't be more simple: You pass any value you want to show on the screen to toStr() and out comes the string representation!
Here is an example:

Code: Select all

obj := {an: "object", just_for: ["testing", "!"]}

msgbox obj        ; this shows an empty message box :(
msgbox toStr(obj) ; this shows: {"an": "object", "just_for": ["testing", "!"]} (just as you would expect it!)
For many more examples check the test directory in the repository.
Keep in mind that toStr() only works in AHK v2, v1 is currently not supported and I, personally, have no plans on porting the script.

Download

Here it is: The GitHub Page
From there you can clone the entire repository, or just download toStr.ahk – it is the only file you need.

Installation

First, download toStr.ahk as explained above.
When you have the file ready, best copy it into your Lib folder. Then you can include it with #include <toStr> in any script you run in the future!
Alternatively, you can copy the file anywhere you like and then use the normal #include to include it in your scripts.

Note

toStr.ahk is licensed under the GNU General Public License v3 and it has to stay that way.
Feel free to use the source code however you like for anything private – I'm not going to hunt you down, even if you you may violate the license terms a bit here and there.
When you want to publish code that makes use of toStr.ahk, however, please do follow what is said in the license.
You can find a fairly easy explanation to the license text here: TLDRlegal.com



In any way, I always get happy when people use my code, so why not just write me a little forum PM or a comment or whatnot if you are using toStr.ahk? It would definitely make me happy! :3

Also – this goes without saying – tell me about any bugs or suggestions!
You can always just open up an Issue or Pull Request on GitHub, but also just comment under this thread. It is very appreciated!

Thank you for your time, I hope you like this script!
gregster
Posts: 9087
Joined: 30 Sep 2013, 06:48

Re: [v2] toStr() – represent anything as a string (for debugging, etc.)

28 Mar 2019, 11:38

That looks useful - in v1.1, I always use Coco's JSON Class (slightly modified, iirc) for assoc. arrays (.dump and .load) - for normal arrays, I have some other function.

That said, a function which transforms the string back into an object would be the perfect addition. This way, you could save objects in text files and re-load them back as objects and, for example, import JSON responses from web APIs as objects into your script for parsing.
User avatar
kczx3
Posts: 1648
Joined: 06 Oct 2015, 21:39

Re: [v2] toStr() – represent anything as a string (for debugging, etc.)

28 Mar 2019, 12:10

JSON isn't for display really. And I don't think the goal of this function is about saving and restoring associative arrays. It is merely because objects do not have a toString() method in AHK.
User avatar
SALZKARTOFFEEEL
Posts: 89
Joined: 10 May 2017, 11:14
Location: Germany
Contact:

Re: [v2] toStr() – represent anything as a string (for debugging, etc.)

28 Mar 2019, 12:20

The purpose of this function is not to store data, however, I could see this being useful too!
Currently, turning strings into data is actually possible using continuation sections. (I can't find the code again, but I will hopefully edit this post once I get it.)
gregster
Posts: 9087
Joined: 30 Sep 2013, 06:48

Re: [v2] toStr() – represent anything as a string (for debugging, etc.)

28 Mar 2019, 12:26

kczx3 wrote:
28 Mar 2019, 12:10
JSON isn't for display really.
My point was not so much about displaying, but saving/loading and being able to treat them as objects in AHK.
And of course, for debugging, the display of JSON/assoc. arrays as text can be really useful. Although I also like HotkeyIt's ObjTree() function for this - especially if you have many levels.
And I don't think the goal of this function is about saving and restoring associative arrays.
Well, it would be a natural next step, if you already have an object as a string. But of course, it is just a suggestion.
User avatar
SALZKARTOFFEEEL
Posts: 89
Joined: 10 May 2017, 11:14
Location: Germany
Contact:

Re: [v2] toStr() – represent anything as a string (for debugging, etc.)

28 Mar 2019, 12:51

gregster wrote: Well, it would be a natural next step, if you already have an object as a string. But of course, it is just a suggestion.
I am thinking about it! Though, this will certainly not be an update to toStr(), but rather an entirely new script.

Return to “Scripts and Functions (v2)”

Who is online

Users browsing this forum: No registered users and 24 guests