b4rdarian
b4rdarian
MModular
Created by b4rdarian on 10/24/2023 in #questions
Confusion with owned integers
I will do the same.
32 replies
MModular
Created by b4rdarian on 10/24/2023 in #questions
Confusion with owned integers
Apparently I am not the first to notice. There is already an issue open https://github.com/modularml/mojo/issues/747. I can see @Weichen-威辰 has added his experience to it.
32 replies
MModular
Created by b4rdarian on 10/24/2023 in #questions
Confusion with owned integers
Good call. Will do.
32 replies
MModular
Created by b4rdarian on 10/24/2023 in #questions
Confusion with owned integers
VSCode's mojo extension is not picking up on the error. Only when trying to compile.
32 replies
MModular
Created by b4rdarian on 10/24/2023 in #questions
Confusion with owned integers
You might be right and it's a bug. I am on an M2 MacBook.
❯ mojo --version
mojo 0.4.0 (9e33b013)
❯ mojo --version
mojo 0.4.0 (9e33b013)
32 replies
MModular
Created by b4rdarian on 10/24/2023 in #questions
Confusion with owned integers
In that integers are copied rather than owned.
32 replies
MModular
Created by b4rdarian on 10/24/2023 in #questions
Confusion with owned integers
Btw I am also basing my understanding on rust documentation on ownership https://doc.rust-lang.org/book/ch04-01-what-is-ownership.html#ownership-and-functions
32 replies
MModular
Created by b4rdarian on 10/24/2023 in #questions
Confusion with owned integers
I can't really say. It might be, all I am saying is that it doesn't work as it's described in the docs for primitive types.
32 replies
MModular
Created by b4rdarian on 10/24/2023 in #questions
Confusion with owned integers
I think the docs need updating / clarification. I believe it relates to stack vs heap. So copying only works for objects stored in the heap.
32 replies
MModular
Created by b4rdarian on 10/24/2023 in #questions
Confusion with owned integers
These will fail compilation if the the commented lines are uncommented
fn owned_and_borrowed_int():
let x: Int = 1234
use_int(x)
take_int(x)
# use_int(x)


fn owned_and_borrowed_bool():
let x: Bool = False
use_bool(x)
take_bool(x)
# use_bool(x)


fn owned_and_borrowed_float():
let x: Float32 = 1234.0
use_float(x)
take_float(x)
# use_float(x)
fn owned_and_borrowed_int():
let x: Int = 1234
use_int(x)
take_int(x)
# use_int(x)


fn owned_and_borrowed_bool():
let x: Bool = False
use_bool(x)
take_bool(x)
# use_bool(x)


fn owned_and_borrowed_float():
let x: Float32 = 1234.0
use_float(x)
take_float(x)
# use_float(x)
32 replies
MModular
Created by b4rdarian on 10/24/2023 in #questions
Confusion with owned integers
Last call will fail as expected if uncommented.
fn owned_and_borrowed_example():
let p = UniquePointer(1234)
use_ptr(p)
take_ptr(p)
use_ptr(p)
take_ptr(p ^)
# This will cause a compile error as `p` is no longer in scope
# The `^` operator ends the lifetime of the value being binded to `p`.
# use_ptr(p)
fn owned_and_borrowed_example():
let p = UniquePointer(1234)
use_ptr(p)
take_ptr(p)
use_ptr(p)
take_ptr(p ^)
# This will cause a compile error as `p` is no longer in scope
# The `^` operator ends the lifetime of the value being binded to `p`.
# use_ptr(p)
32 replies
MModular
Created by b4rdarian on 10/24/2023 in #questions
Confusion with owned integers
From the example
fn take_ptr(owned p: UniquePointer):
print_no_newline("take_ptr: ")
print(p.ptr)


fn use_ptr(borrowed p: UniquePointer):
print_no_newline("use_ptr: ")
print(p.ptr)
fn take_ptr(owned p: UniquePointer):
print_no_newline("take_ptr: ")
print(p.ptr)


fn use_ptr(borrowed p: UniquePointer):
print_no_newline("use_ptr: ")
print(p.ptr)
32 replies
MModular
Created by b4rdarian on 10/24/2023 in #questions
Confusion with owned integers
Added a __copyinit__ to UniquePointer to allow copying.
struct UniquePointer:
var ptr: Int

fn __init__(inout self, ptr: Int):
self.ptr = ptr

# Allows us to move ownership, eg owned_argument(UniquePointer(123)^)
fn __moveinit__(inout self, owned existing: Self):
self.ptr = existing.ptr

# Allows us to pass by copy, eg owned_argument(UniquePointer(123))
fn __copyinit__(inout self, existing: Self):
self.ptr = existing.ptr

fn __del__(owned self):
self.ptr = 0
struct UniquePointer:
var ptr: Int

fn __init__(inout self, ptr: Int):
self.ptr = ptr

# Allows us to move ownership, eg owned_argument(UniquePointer(123)^)
fn __moveinit__(inout self, owned existing: Self):
self.ptr = existing.ptr

# Allows us to pass by copy, eg owned_argument(UniquePointer(123))
fn __copyinit__(inout self, existing: Self):
self.ptr = existing.ptr

fn __del__(owned self):
self.ptr = 0
32 replies
MModular
Created by b4rdarian on 10/24/2023 in #questions
Confusion with owned integers
I suspect it relates to how Ints are actually stored under the hood with comparison to String objects. I played a bit with another example from the docs (https://docs.modular.com/mojo/programming-manual.html#transfer-arguments-owned-and) yesterday,
32 replies
MModular
Created by b4rdarian on 10/24/2023 in #questions
Confusion with owned integers
I think the compiler's behavior on this isn't fully fleshed out, or blocked on traits, and that's why it worked with String when it ideally shouldn't
The example with the String was taken from the docs. Either the docs are wrong or I am missing something about how owned should behave. Further down in the example it talks about how using ^ actually gives complete ownership, making the passed variable out of scope.
32 replies
MModular
Created by b4rdarian on 10/24/2023 in #questions
Confusion with owned integers
Btw the error is similar to what I get if I call
take_ownership_str(hello^)
print(hello)
take_ownership_str(hello^)
print(hello)
32 replies