ModularM
Modular13mo ago
1 reply
gphlipot

Conditional Conformance Based on Origin

I'm trying to get a better understanding of origins and mutability. I'd like to try and write a function that have a mutable origin. For example, I'd like something like:

from memory.pointer import Pointer

@value
struct Foo[mut: Bool, //, *, origin: Origin[mut]]:
    var _data: Pointer[type=Int, origin=origin]

    fn set(self: Foo[origin=MutableAnyOrigin], i: Int):
        self._data[] = i


fn foo(mut f: Foo, val: Int):
    f.set(val)


The code above doesn't compile, and gives the following error:

error: invalid call to 'set': self argument cannot be converted from 'Foo[origin=origin]' to 'Foo[origin=MutableAnyOrigin]'
    f.set(val)
    ~~~~~^~~~~


Does anyone know if this is possible in the language?
Was this page helpful?