fubark / cyber

Fast and concurrent scripting.
https://cyberscript.dev
MIT License
1.21k stars 43 forks source link

Should toString() be added to number? #33

Closed sts10 closed 1 year ago

sts10 commented 1 year ago

Kind of the reverse of #31. I tried:

four = 4
four_as_string = four.toString()

in the playground and get following error:

panic: Missing method symbol `toString` from receiver of type `number`.
main:2:18 main:
four_as_string = four.toString()
^

I guess it's a rare case to want to convert a number to a string, but I think it should be possible.

fubark commented 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.

sts10 commented 1 year ago

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()?

fubark commented 1 year ago

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.

sts10 commented 1 year ago

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.

fubark commented 1 year ago

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.

fubark commented 1 year ago

Just pushed an update for this. Also fixed some formatting issues with number and raw string slice.