M
Modular13mo 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 199713mo 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
TeamPuzel13mo 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
TeamPuzel13mo 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 199713mo ago
The really interesting part is that I have yet to encounter anything that requires using the __moveinit__ method.
TeamPuzel
TeamPuzel13mo 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 199713mo 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
TeamPuzel13mo ago
I'm pretty sure I got compile errors did you use @value ?
mad alex 1997
mad alex 199713mo 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
TeamPuzel13mo 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
TeamPuzel13mo ago
I don't know what you're doing differently
No description
TeamPuzel
TeamPuzel13mo ago
commenting out moveinit does break my code
TeamPuzel
TeamPuzel13mo ago
Whereas when I add it back it works.
No description
TeamPuzel
TeamPuzel13mo 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