Closed sts10 closed 1 year ago
You can achieve this with string(four)
. I'm open to adding this down the road if people like toString(). One issue I can see is it'll give the impression that every value has toString() and that would require adding a missing function fallback that may reduce performance.
Ah, thank you.
For what it's worth, I think string(4)
is more in-line with how number("4")
and int("4")
work currently. Is there a reason we still need foo.toString()
? In other words, could you add the functionality of foo.to_String()
into string(foo)
, then deprecate .toString()
?
toString() doesn't exist at the moment (Except for rawstring to get back a valid utf8 string). I'm hesitant to add it to number because it would lead to other primitives to have it too.
Except for rawstring to get back a valid utf8 string
Ah OK. I guess what I was suggesting is to allow users to use string(rawstring)
to get back a valid utf8 string. Then Cyber could do away with toString()
all together.
Broadly, I think having string(foo)
and foo.toString()
in the docs could confuse new users. But I'm obviously not aware of the interior mechanics and whether this is easy or difficult.
Gotcha, yea to add to this, I think we should rename toString()
to utf8()
. That function would do validation and can return an error. string()
on the otherhand doesn't return errors and does it's best to get back something. Right now I think that behavior is to return "rawstring (20)" where 20 is the length and it does something similar with other object types. One alternative could be: it does validation and either returns the utf8 or an empty string upon error.
Just pushed an update for this. Also fixed some formatting issues with number and raw string slice.
Kind of the reverse of #31. I tried:
in the playground and get following error:
I guess it's a rare case to want to convert a number to a string, but I think it should be possible.