I'm looking for the Nim equivalent for dir() in python, which returns a list of properties and bound methods.
For example, dir(str) would return a list like ['__add__', ...,'split',...] which is very handy to find ways to manipulate the object. The Nim equivalent might be a combination of "a proc returning a list of props" and "a proc that returns a list that the type can be a param of".
When you are looking for all the string "methods" you may go to
https://nim-lang.org/docs/system.html
and select "Group by Type". Then procs are listed grouped by types.
Thank you for your reply. They seem very handy as a document but that is not what I want.
I'm supposing a situation, for example, where I import several libraries (with some poorly documented) and would like to know whether a given proc is defined for a given object. (Of course, I can try proc(object) for this specific task, but this is merely an example of what I would like to use dir for.
Is it possible to use metaprogramming to iterate through all of the available procs, and then return them as a list or tuple at runtime?
I think this is how both "locals()" and "repr()" already work in Nim.
I've learned a bit more since then (now also on last chapter of Nim in Action. Templates and Macros are also starting to make more sense).
Also a bit of a better idea of the fine line between where metaprogramming vs actual compiler internals lie, where you can and can't cross the two.
Probably you'd need to make a nim compiler plugin (this is how "locals()" is implemented). I think this concept is similar to rust's "syntax extensions".
Relevant previous forum thread on the subject (it's not really documented anywhere that I can see as a type of Nim metaprogramming):
https://forum.nim-lang.org/t/1220
So for instance, it's _probably possible to crossreference this:
compiler/plugins/locals.nim
Alongside the nimsuggest source code (included in Nim github repo)
And add something like these new plugins, to get something a bit more pythonish:
compiler/plugins/globals.nim compiler/plugins/dir.nim
Here's a very very stupid nimdir.sh script that I find useful:
#!/bin/sh
libPath=~/.choosenim/toolchains/nim-0.18.0/lib
# libPath=$(choosenim --noColor show | grep ' Path: ' | awk '{print $2}')/lib
nimFile=$(find $libPath -iname $1.nim | sed 1q)
[ -f "$nimFile" ] || exit 1
echo "outline $nimFile" | nimsuggest $nimFile 2>/dev/null |\
grep '^outline' | awk '{print $3 " " $2}'
You could in theory write a nim macro that calls nimsuggest and parses the output. A much better solution would be to have a nim module that does this internally.