Mojo trait
FloatableRaising
The FloatableRaising
trait describes a type that can be converted to a Float64, but the conversion might raise an error (e.g.: a string).
This trait requires the type to implement the __float__()
method, which
can raise an error.
For example:
from utils import Variant
struct MaybeFloat(FloatableRaising):
var value: Variant[Float64, NoneType]
fn __float__(self) raises -> Float64:
if self.value.isa[NoneType]():
raise "Float expected"
return self.value[Float64]
A MaybeFloat
can now be converted to Float64
:
try:
print(Float64(MaybeFloat(4.6)))
except:
print("error occurred")
Implemented traits
AnyType
,
UnknownDestructibility
Aliases
__del__is_trivial
alias __del__is_trivial
A flag (often compiler generated) to indicate whether the implementation of __del__
is trivial.
The implementation of __del__
is considered to be trivial if:
- The struct has a compiler-generated trivial destructor and all its fields
have a trivial
__del__
method.
In practice, it means that the __del__
can be considered as no-op.
Methods
__float__
__float__(self: _Self) -> SIMD[float64, 1]
Get the float point representation of the value.
Returns:
SIMD
: The float point representation of the value.
Raises:
If the type does not have a float point representation.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!