You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
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 asint
,float
,complex
,bool
, ...) is exactly the same. If you try to assignint
to afloat
, it will be a compile time error. The reason is that in CPython the annotation is ignored, and the type ofa
will be that ofb
, it will be just a "reference counted pointer", botha
andb
pointing to the same object.However, if both
a
andb
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.The text was updated successfully, but these errors were encountered: