ModularM
Modular3y ago
5 replies
JIMC

`if` Statement doesn't work??? :astonished:

Consider this:
@register_passable("trivial")
struct U69:
    var value: __mlir_type.ui69

    alias MAX: Int = 590295810358705651711
    alias ZERO: Int = 0

    alias greater_than_255_err_msg: StringLiteral = "Unable to parameterize a value of type `U69` with an integer value greater than 590295810358705651711"
    alias lesser_than_0_err_msg: StringLiteral = "Unable to parameterize a value of type `U69` with a negative integer value"

    @always_inline("nodebug")
    fn __init__(N: Int) raises -> Self:
        print("In `__init__`, `N` is: ", N)
        if N > Self.MAX:
            raise Error(Self.greater_than_255_err_msg)
        if N < Self.ZERO:
            raise Error(Self.lesser_than_0_err_msg)
        return Self {
            value: __mlir_op.`index.castu`[_type = __mlir_type.ui69](N.__mlir_index__())
        }

    @always_inline("nodebug")
    fn __into_int__(owned self) -> Int:
        return __mlir_op.`index.castu`[_type = __mlir_type.index](self.value)

    @always_inline("nodebug")
    fn __into_mlir_u69__(self) -> __mlir_type.ui69:
        return self.value

    @always_inline("nodebug")
    fn __add__(self: Self, other: Self) raises -> Self:
        let lhs = __mlir_op.`index.castu`[_type = __mlir_type.index](self.__into_mlir_u69__())
        let rhs = __mlir_op.`index.castu`[_type = __mlir_type.index](other.__into_mlir_u69__())
        let result = __mlir_op.`index.add`[](lhs, rhs)
        return U69(Int(result))


fn main() raises:
    let c: U69 = 2
    print(c.__into_int__())

Then run mojo, I got this:
mojo src/u69.mojo
In `__init__`, `N` is:  2
Unhandled exception caught during execution: Unable to parameterize a value of type `U69` with an integer value greater than 590295810358705651711
mojo: error: execution exited with a non-zero result: 1


Hmmm, 2 is definitely lesser than Self.MAX=590295810358705651711, what happened? @ModularStaff
Was this page helpful?