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

Doc: document how casting works in LPython #293

Open
certik opened this issue Mar 27, 2022 · 0 comments
Open

Doc: document how casting works in LPython #293

certik opened this issue Mar 27, 2022 · 0 comments

Comments

@certik
Copy link
Contributor

certik commented Mar 27, 2022

We should add LPython specific documenation (Sphinx+Myst). Among other things, document how casting works.

For assignments a = b, LPython ensures that the type (such as int, float, complex, bool, ...) is exactly the same. If you try to assign int to a float, it will be a compile time error. The reason is that in CPython the annotation is ignored, and the type of a will be that of b, it will be just a "reference counted pointer", both a and b pointing to the same object.

However, if both a and b are the same type, say, int, then if they are different kind (say one is 32bit, the other 64bit), then LPython automatically inserts an implicit cast. The reason for this behavior is that CPython only has a type int, an arbitrary precision integer. LPython subdivides this type to i8, i16, i32, i64 (and we'll add more in the future, including an arbitrary precision int), and it is LPython's responsibility to ensure that things get casted correctly, and that the integer type can hold the actual integer. So in Debug mode, the ImplicitCast node from a larger integer to a smaller integer, say 64bit to 32bit must also insert a runtime check that the integer fits. If it does not fit, it should give a runtime error. (In Release mode it will produce incorrect answers.) That way if you run your code in Debug mode and get no runtime errors, you know it will behave exactly as in CPython.

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

1 participant