mojo icon indicating copy to clipboard operation
mojo copied to clipboard

[BUG]: cannot be converted from 'fn(Person) -> Person' to 'fn(Person) -> Person'

Open sa- opened this issue 1 year ago • 6 comments

Bug description

I was trying to make a small implementation for a list that supports memory only types and I came across a bug - not sure if this is a known issue or not. The code throws an error that I don't quite understand

`method argument #0 cannot be converted from 'fn(Person) -> Person' to 'fn(Person) -> Person'`

Steps to reproduce

from Vector import DynamicVector
from List import VariadicList
from String import String

@value
struct List[T: AnyType]:
    var data: DynamicVector[Pointer[T]]
    
    fn __init__(inout self, *args: T):
        let args_list: VariadicList[T] = args
        var dv = DynamicVector[Pointer[T]](args_list.__len__())
        for i in range(args_list.__len__()):
            var value = args_list[i]
            let ptr = Pointer[T].address_of(value)
            dv.push_back(ptr)
        self.data = dv

    fn map[T_return: AnyType](self, f: fn(T) -> T_return) -> List[T_return]:
        var new_vec = DynamicVector[Pointer[T_return]](self.data.size)
        for i in range(self.data.size):
            let old_value = self.data[i].load()
            var new_value = f(old_value)
            
            let ptr = Pointer[T_return].address_of(new_value)
            new_vec.push_back(ptr)
            
        return List[T_return](new_vec)
    
    fn __getitem__(self, i: Int) -> T:
        let value = self.data[i].load()
        return value

@value
struct Person:
    var name: String
    var age: Int


let listy_mclistface = List(Person("Bob", 1))

fn birthday(p: Person) -> Person:
    return Person(p.name, p.age+1)

fn is_bob(p: Person) -> Bool:
    return p.name == "Bob"

## Running the code
listy_mclistface
    .map[Person](birthday) # also fails when I don't specify the Person type param
    .filter(is_bob)

And this is the error message:

error: Expression [1]:62:21: invalid call to 'map': method argument #0 cannot be converted from 'fn(Person) -> Person' to 'fn(Person) -> Person'
        .map[Person](birthday) # also fails when I don't specify the Person type param
~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~

Expression [1]:21:5: function declared here
    fn map[T_return: AnyType](self, f: fn(T) -> T_return) -> List[T_return]:
    ^


### System information

_No response_

sa- avatar May 27 '23 15:05 sa-