ModularM
Modular3y ago
4 replies
Dania

Why doesn't __bool__ allow explicit conversion?

I am playing with "Low level IR" level and it works great. However when i try to print out OurBool i get errors stating that OurBool can not be converted to Bool despite having a __bool__ method.
I am using Mojo 6.1
@register_passable("trivial")
struct OurBool:
    var value: __mlir_type.i1

    fn __init__(value: __mlir_type.i1) -> Self:
        return Self {value: value}

    fn __bool__(self) -> Bool:
        return Bool(self.value)


fn main():
    let a = OurBool(__mlir_attr.`true`)
    if a:
        print('a is True')
        # This works
    
    print(a)
    # This does not:
    # /home/danil/source/mjtest/main.mojo:37:10: error: no matching function in call to 'print':
    # print(a)
    # ...
    # /home/danil/source/mjtest/main.mojo:1:1: note: candidate not viable: argument #0 cannot be converted from 'OurBool' to 'Bool'
    # alias OurTrue = OurBool(__mlir_attr.`true`)
    
    print(Bool(a))
    # The explicit conversion does not work too
Was this page helpful?