Closed RichardCoppin closed 4 days ago
Unless there is an intentional reason why this hasn't already been implemented, I can get started on this.
Looks like this was already committed with: https://github.com/modularml/mojo/pull/3163
This is available in nightly builds.
Review Mojo's priorities
What is your request?
Currently
float()
behaves weirdly in VSCode IDE. It seems to be identified as a keyword by the LSP (I assume), but then when you try to use it, we get the erroruse of unknown declaration 'float'
:We also do not have a way to denote a class which can be converted to a floating point value.
Could we not just have something similar to the integer class:
What is your motivation for this change?
Floating point values seem to be a bit of a sharp edge in Mojo at the moment.
The
float
token is also exhibiting contradictory behavior. VSCode identifies it as a keyword, but the compiler does not have a definition of it.When constructing your own struct, which should be convertible into float, there is no simple way of casting to floating point:
To extract the floating point value of the struct, one would need to do something like:
Trying to do a mere conversion without the clunky dunder call, just results in an error:
Yet with the above
I can just call :
or if the FloatXX methods were aware of the 'Floatable' trait it could just be:
Any other details?
The dunder method
def __float__(self)__ -> float
is a defined __dunder__ method in Python.So this would be inline with expected behavior.
I cannot say which would be the preferred bit-depth for return of the float() method. It could be that this should be compilation target specific.