Cant for the life of me figure it out. Probably doesn't help I am little new to this. I know its simple but I cant see it. Help would be appreciated. Sorry in advance for the newbyness.
import os, iup
#Super Spy
proc chrome(): string =
var x: string
x="test"
proc doc(): string =
var x: string
x="string"
block menu:
echo """
_________ _________
/ _____/__ ________ ___________ / _____/_____ ___.__.
\_____ \| | \____ \_/ __ \_ __ \ \_____ \\____ < | |
/ \ | / |_> > ___/| | \/ / \ |_> >___ |
/_______ /____/| __/ \___ >__| /_______ / __// ____|
\/ |__| \/ \/|__| \/
Welcome to Super Spy please slecet your option.
1. Chrome Grabber
2. Documents Grabber"""
case readLine(stdin)
of "1":
chrome()
of "2":
doc()
else: echo "Please pick a real option."
And it produces this.
Hint: system [Processing]
Hint: grabber [Processing]
Hint: os [Processing]
Hint: strutils [Processing]
Hint: parseutils [Processing]
Hint: math [Processing]
Hint: algorithm [Processing]
Hint: times [Processing]
Hint: winlean [Processing]
Hint: dynlib [Processing]
Hint: ospaths [Processing]
Hint: iup [Processing]
grabber.nim(23, 15) Error: expression 'chrome()' is of type 'string' and has to be discarded
Try this:
import os, iup
#Super Spy
proc chrome(): string =
result = "test" # `result` is implicitly available and has the return type
proc doc(): string =
result = "string"
block menu:
echo """
_________ _________
/ _____/__ ________ ___________ / _____/_____ ___.__.
\_____ \| | \____ \_/ __ \_ __ \ \_____ \\____ < | |
/ \ | / |_> > ___/| | \/ / \ |_> >___ |
/_______ /____/| __/ \___ >__| /_______ / __// ____|
\/ |__| \/ \/|__| \/
Welcome to Super Spy please slecet your option.
1. Chrome Grabber
2. Documents Grabber"""
case readLine(stdin)
of "1":2
echo chrome()
of "2":
echo doc()
else: echo "Please pick a real option."
Well your proc chrome() returns a string. In Nim returned values are not silently discarded by default, unless you apply discardable pragma to the proc.
So you can write in your code
echo chrome()
var myStr = chrome()
discard chrome()
Or, if you really intent to be able to discard the result, then see Nim manual for discardable pragma. Maybe chrome() proc is not really intended to return a result at all, so fix that proc.
There are two errors one can say. First of all, you call chrome() and doc() but you don't do anything with the string that they return. Add echo before like echo chrome() at least.
Secondly, the procs don't actually return the string, you just create a local var x and assign it, but you forgot to return it. Now... procs have an implicit variable called result that you don't need to declare, so you can instead write:
proc chrome(): string =
result = "string"
...but in fact, you can also make the last line be an expression and Nim will return that implicitly:
proc chrome(): string =
"string"
With those small changes it works :)
In first place I am just curious why chrome and doc procedures return strings? If you don't need result of these procedures just don't set type of return value:
proc chrome =
var x: string
x = "chrome"
proc doc =
var x: string
x = "doc"
But if you need and set return values, yes: you should use them.
Just for example I want to suggest a variant with "case" as expression:
proc chrome(): string =
var x: string
x = "test"
"test chrome"
# or
# return "test chrome"
# or
# result = "test chrome"
proc doc(): string =
var x: string
x="string"
"test doc"
...
block menu:
...
echo case readLine(stdin)
of "1":
chrome()
of "2":
doc()
else:
"Please pick a real option."