Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should toString() be added to number? #33

Closed
sts10 opened this issue Jan 30, 2023 · 6 comments
Closed

Should toString() be added to number? #33

sts10 opened this issue Jan 30, 2023 · 6 comments

Comments

@sts10
Copy link
Contributor

sts10 commented Jan 30, 2023

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
Copy link
Owner

fubark commented Jan 30, 2023

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.

@fubark fubark changed the title Trouble converting number toString() Should toString() be added to number? Jan 30, 2023
@sts10
Copy link
Contributor Author

sts10 commented Jan 30, 2023

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
Copy link
Owner

fubark commented Jan 30, 2023

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
Copy link
Contributor Author

sts10 commented Jan 30, 2023

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
Copy link
Owner

fubark commented Jan 30, 2023

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
Copy link
Owner

fubark commented Jan 31, 2023

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

@fubark fubark closed this as completed Jan 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants