ModularM
Modular2y ago
9 replies
Anindya

How to declare a function type in a trait?

This is a simple class in Python

class MyPair:
    def __init__(first, second):
        self.first = first 
        self.second = second

        def _hello():
            return None 
        
        self._hi = _hello 


Now I thought I could this in Mojo in someway but stucked

struct MyPair:
    var first: Int
    var second: Int
    var _hi 

    fn __init__(inout self, first: Int, second: Int):
        self.first = first
        self.second = second
        fn _hello():
            return None 
        
        self._hi = _hello 


Is there any possible way of doing this in Mojo?

Error:

error: Expression [17] wrapper:22:13: struct field declaration must have a type
    var _hi 
            ^
error: Expression [17]:12:13: 'MyPair' value has no attribute '_hi'
        self._hi = _hello 
Was this page helpful?