In my Clarity smart contract, I am trying to to append one string ("Hello") to another string (" to Clarity Language"). Both strings are of type string-utf8
.
Deploying the contract below fails with an error: expecting expression of type \'(string-utf8 100)\', found \'(string-utf8 120)\'
(define-data-var a-string (string-utf8 100) u"Hello")
(var-set a-string (concat (var-get a-string) u" to Clarity Language"))
(print (var-get a-string))
How to make this work?
concat
does not optimize the resulting string. The new string is of type string-utf8 with length 120, adding the length of the variable type to the length of the other string (100 + 20).You have to wrap the
concat
call with aas-max-len?
:Note, that the type length is defined by an
int
(100
), whileas-max-len?
takes auint
parameter (u100
).