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

Implement more conversions for primitive types #174

Closed
faultyserver opened this issue Mar 24, 2018 · 0 comments
Closed

Implement more conversions for primitive types #174

faultyserver opened this issue Mar 24, 2018 · 0 comments
Labels
feature-request Any request for a new feature of the language. Includes both syntax and library features. good first issue An issue that provides a good intro to working with the Myst codebase. Be helpful! nativelib Any issue relating to the native library (Crystal code) of Myst.
Milestone

Comments

@faultyserver
Copy link
Member

Recently I needed to convert an Integer to a Float for a calculation to work properly, but I realized there's no method to do this directly. Instead, I had to do 1.0 * iterations to convert the value.

Having conversion methods between primitive types would make this much more clear and is generally expected by most people coming from Ruby or Crystal.

Conversions I think need implementing (not necessarily complete):

  • Integer#to_f
  • Float#to_i (should truncate the value, not round)
  • Float#round (should round to the nearest Integer value, e.g. 0.5 would round to 1)
  • String#to_i (see Crystal's API)
  • String#to_f (see Crystal's API)

For an example of how these methods can be implemented as "passthroughs", see examples likeFloat#to_s, which just calls Crystal's to_s on the receiving value:

NativeLib.method :float_to_s, Float64 do
this.to_s
end

@faultyserver faultyserver added good first issue An issue that provides a good intro to working with the Myst codebase. Be helpful! feature-request Any request for a new feature of the language. Includes both syntax and library features. nativelib Any issue relating to the native library (Crystal code) of Myst. labels Mar 24, 2018
faultyserver added a commit to faultyserver/myst that referenced this issue Mar 31, 2018
…imitive values.

This includes `Integer#to_f`, `Float#to_i`, `Float#round`, `String#to_i`, and `String#to_f`.

These are essentially passthroughs to the native Crystal methods. The String methods, which take arguments in the Crystal version, are implemented with all of the flags enabled for simplicity. This will likely change when named arguments are allowed.
@faultyserver faultyserver added this to the Next milestone Mar 31, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request Any request for a new feature of the language. Includes both syntax and library features. good first issue An issue that provides a good intro to working with the Myst codebase. Be helpful! nativelib Any issue relating to the native library (Crystal code) of Myst.
Projects
None yet
Development

No branches or pull requests

1 participant