Hi, beginner here. Consider the following code:
import winim
let myRange = 0..99
proc playAndWriteLN(audioFile, myStr: string) =
# PlaySound is a procedure from the winim package, available on Windows only
PlaySound(audioFile, 0, SND_NOSTOP)
echo(myStr)
this works as long as I pass a single string to be written by echo, e.g.:
playAndWriteLN("C:/winvox/Sound/Sortevox/So_sorte.wav", "The range is that one.")
But it stops working as soon as I want to pass more than one parameter to echo, e.g.
playAndWriteLN("C:/winvox/Sound/Sortevox/So_sorte.wav", "The range is between ", myRange.a, " and ", myRange.b)
How do I solve this? I tried the following at playAndWriteLN signature:
proc playAndWriteLN(audioFile: string; myStr: varargs[string, `$`]) =
Then it works, except that it prints quotes and brackets to the screen. How can I command it to print like a normal echo, one that is outside any procedure?
thanks much.
import std/strutils
proc playAndWriteLN(audioFile, myStr: varargs[string, `$`]) =
# PlaySound(audioFile, 0, SND_NOSTOP)
echo(myStr.join)
playAndWriteLN("The range is between ", 0, " and ", 99)
(the forum doesn't like my formatting today for some reason)
Try:
proc playAndWriteLN(audioFile: string; myStr: varargs[typed, `$`]) =
But maybe this then needs to become a macro, not sure right now.
I wrote the solution with Araq's hint. It did not work for me with a procedure instead of a macro.
import macros, winim
let myRange = 0..99
macro playAndWriteLN(audioFile: string; myStr: varargs[typed]): untyped =
result = newStmtList()
# Add PlaySound call to result
let playStmt = quote do:
PlaySound(`audioFile`, 0, SND_NOSTOP)
result.add(playStmt)
# Add echo call to result
let echoStmt = newCall("echo")
for s in myStr:
echoStmt.add(s)
result.add(echoStmt)
playAndWriteLN("C:/winvox/Sound/Sortevox/So_sorte.wav", "The range is between ", myRange.a, " and ", myRange.b)
A template is sufficient:
import std/macros
template playAndWriteLN(audioFile: string; myStr: varargs[typed]) =
PlaySound(`audioFile`, 0, SND_NOSTOP)
echo.unpackVarargs(myStr)