M
Modular15mo ago
Mr J

Is inout a equivalent system to pass a pointer to the function?

I was wondering if inout is a equivalent system to pass a pointer to the function?
13 Replies
mad alex 1997
mad alex 199715mo ago
It makes a variable mutable in the scope of the function. So if you pass an Int into a function and add to it it will maintain its new value in the outer scope. inouting a variable is almost universally faster than creating one in the scope passing it out.
TeamPuzel
TeamPuzel15mo ago
quoting the documentation: https://docs.modular.com/mojo/programming-manual.html#mutable-arguments-inout "Notice that we don’t call this argument passing “by reference.” Although the inout convention is conceptually the same, we don’t call it by-reference passing because the implementation may actually pass values using pointers."
Modular Docs - Mojo🔥 programming manual
A tour of major Mojo language features with code examples.
TeamPuzel
TeamPuzel15mo ago
Everything is said in a way that implies you're not supposed to make assumptions about the actual implementation
mad alex 1997
mad alex 199715mo ago
The really interesting part is that I have yet to encounter anything that requires using the __moveinit__ method.
TeamPuzel
TeamPuzel15mo ago
what do you mean? It is required for move only types Like a socket If you copied a socket the destructors would try close it multiple times things you would usually use classes for
mad alex 1997
mad alex 199715mo ago
I don't think Mojo has the apparatus to check borrowing yet is what I am saying. I have used inout, and owned and never had to add a __moveinit__ to my array struct.
TeamPuzel
TeamPuzel15mo ago
I'm pretty sure I got compile errors did you use @value ?
mad alex 1997
mad alex 199715mo ago
Though I haven't tried using async which is the only place it would matter. No I don't use value unless i need everything it adds.
TeamPuzel
TeamPuzel15mo ago
This is odd, I have to try remove it but I'm pretty sure you just don't need moveinit if you have a copyinit
TeamPuzel
TeamPuzel15mo ago
I don't know what you're doing differently
No description
TeamPuzel
TeamPuzel15mo ago
commenting out moveinit does break my code
TeamPuzel
TeamPuzel15mo ago
Whereas when I add it back it works.
No description
TeamPuzel
TeamPuzel15mo ago
Copying a struct with pointers inside would lead to a double free, though in this case I made a reference counted smart pointer so the only consequence would be shared mutable state.
Want results from more Discord servers?
Add your server