I want to concatenate a string and int.
I tried doing
echo "abc" & 2
But it doesn't seem to work. One possible solution is to convert int to string and then do the concatenation. But the value which I have is BigInt not int. So conversion is not possible.
It will never work fine. As & operator doesn't work with integers. Integer need to be stringified.
echo "abc" & $2
the above one works. Just figured that out.
Seems to work fine with bigints module: https://github.com/def-/nim-bigints
import bigints
var x = initBigInt("861210082732066541532665887925299303579512781038289409489470886836236995532341661502300077529662592103949902897170235705942880440590653121006900618559776768310816610553371337447706999981577599375587341186749682484082350039871662512708747027935794011364002882479290935571588018504174265039105934908825594902472830871614778292875382370917452638932983308796613574868097761220057870728168147915772008453100610713188736271915262792413408620656544871809262305724051465756524682416577887280505693119967339110566033625292183037852238710834633719668887688918151471745620796224928388087679593585646815738655018105979481430851232357068527760908700832334635544169865534008118045857792961830603167249028302420612456968287955659132951856711104091884763609192744199821311720800038359880293199593290902312150930776977888986212927451017185789241644617568344066866602117874714001907745824210773602563102098490105135858430074335956305731087923049926757812500")
echo "abc: ", x
@Stefan_Salewski, I was trying to read a num value from jsonNode using
proc getNum(n: JsonNode; default: BiggestInt = 0): BiggestInt {..}
which is a part of json module. Hence when you concatenate without converting it to string it doesn't work. As I am new to nim I thought I could do such concatenation. But now I know I need to stringify the int first.
although $ is defined for BigInt, & is not, so the following works because echo uses $ (toString) for each argument (read the documentation)
echo "abc: ", x
To make the following work
echo "abc" & 2
We need to define & for string type to accept int concatenation (by operator overloading)
proc `&`(s: string, i: int) : string = s & $i # string & int
proc `&`(i: int, s: string) : string = $i & s # int & string
var number = 100
echo number & " --- " & number