Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic string formatting with NIM

Tags:

nim-lang

I am trying to do some very basic string formatting and I got immediately stuck.

What is wrong with this code?

import strutils
import parseopt2

for kind, key, val in getopt():
    echo "$1 $2 $3" % [kind, key, val]

I get Error: type mismatch: got (TaintedString) but expected 'CmdLineKind = enum' but I don't understand how shall I fix it.

like image 396
alec_djinn Avatar asked Feb 27 '17 11:02

alec_djinn


Video Answer


1 Answers

The problem here is that Nim's formatting operator % expects an array of objects with the same type. Since the first element of the array here has the CmdLineKind enum type, the compiler expects the rest of the elements to have the same type. Obviously, what you really want is all of the elements to have the string type and you can enforce this by explicitly converting the first paramter to string (with the $ operator).

import strutils
import parseopt2

for kind, key, val in getopt():
  echo "$1 $2 $3" % [$kind, key, val]

In case, you are also wondering what is this TaintedString type appearing in the error message, this is a special type indicating a non-validated external input to the program. Since non-validated input data poses a security risk, the language supports a special "taint mode", which helps you keep track of where the inputs may need validation. This mode is inspired by a similar set of features available in the Perl programming language:

http://docstore.mik.ua/orelly/linux/cgi/ch08_04.htm

like image 180
zah Avatar answered Oct 26 '22 14:10

zah