Hello,
While exploring I was trying to discover the type, kind of results returned from a proc.
var results = execShellCmd("ls")
I tried many things to find the type of the results. type, typeinfo, typedesc, kind, ...
I haven't found a way.
Any help greatly appreciated.
Thanks.
import typetraits
var results = execShellCmd("ls")
echo type(results)
var x: type(result)
I would do:
import typetraits
var results = execShellCmd("ls")
echo name(results.type)
Isn't ``name`` or ``$`` needed?
Thanks for the information.
What confuses me on some of this is that when I used execShellCmd(*) it did what I wanted done. The returned value when displayed via echo(results) displayed what I wanted to use. But I see no way to get what is displayed. I do not understand why echo() can get it, but I can not.
I was hoping that by learning the type returned that I would learn how I could access the information that was being displayed, but I was failing to be able to use. I still don't understand how in this case an int displays what echo() displays?
Regardless this does answer my original question. Thanks and thanks for the additional examples.
The returned value when displayed via echo(results) displayed what I wanted to use. But I see no way to get what is displayed. I do not understand why echo() can get it, but I can not.
I didn't understand that statement. What was it was echo could display but you couldn't get?
You don't have to use typetraits to understand the return value of that proc. The proc signature tells that it returns the exit status code of the command being run. See https://nim-lang.org/docs/os.html#execShellCmd,string. So you should expect either a 0 or a 1 as the return value.
In general the simplest way to find a type of something in a typed language, if you don't have access to an editor that integrates with it (for Nim I suggest visual studio code + the Nim extension) is to just put a fake type in there and look at the error. If you try, say,
import os
var x: float = execShellCmd("ls")
you will get the error Error: type mismatch: got <int> but expected 'float' which shows that execShellCmd returns an int.
Of course, an actual integrated editor would show you the type on mouseover, which is much more convenient
Thanks for all the replies.
@kaushalmodi
What it appears that was happening and was confusing me, was that I would have the code similar to
var cmdresult = execShellCmd("ls")
echo(cmdresult) #Display the result so that I can see what is happening.
Since I called echo(cmdresult) immediately after the call to execShellCmd() it was not discernable that what was displaying was not due to my call to echo, but due to my call to execShellCmd().
At least to me with my current understanding of Nim. It is not readily apparent from the docs that execShellCmd was going to display anything. So I thought it was my call to echo. I could see from the docs that it returned an int. But from my experiments it appeared to be a string. Thanks for helping with that.
@alehander42 Thanks. That got me going.
@andrea Thanks for the suggestion. I am currently using TextAdept. It isn't to bad, but is not nearly as nice as VisualStudio. I am not interested in VisualStudio. I prefer open source solutions and have 20+ years of disliking Microsoft. :)
I have looked at the source for some things. But some things say Any. So to my understanding it isn't possibly until compile or runtime when that is resolved.
Your little hack for discovering a type is interesting. I will keep that in mind.
Thanks again to everyone for helping on my journey.
@Tiberium
Thanks for clarifying the difference between Visual Studio and Visual Studio Code. I am glad it is an open source product. I still have difficulty trusting Microsoft and especially concerning privacy. I would like to be wrong. But trust has to be earned. For me that has not happened.