ivellapillil
ivellapillil
MModular
Created by ivellapillil on 10/24/2024 in #questions
Preserving mutability when passing to function
How to make pointer passed to a function preserve/transmit its mutability?
var a_instance = A(12)
var ref_a = Pointer.address_of(a_instance)

def change_value(p: Pointer[A]):
p[] = 15 # <- Error

change_value(ref_a)
ref_a[] = 15 # Works
var a_instance = A(12)
var ref_a = Pointer.address_of(a_instance)

def change_value(p: Pointer[A]):
p[] = 15 # <- Error

change_value(ref_a)
ref_a[] = 15 # Works
Or is it a conscious constraint on Pointer and we should use UnsafePointer or Arc here?
42 replies
MModular
Created by ivellapillil on 7/23/2024 in #questions
Bug or optimisation
The following code:
struct A:
fn __init__(inout self): ...

fn main():
print(UnsafePointer.address_of(A()) == UnsafePointer.address_of(A())) # Prints True
struct A:
fn __init__(inout self): ...

fn main():
print(UnsafePointer.address_of(A()) == UnsafePointer.address_of(A())) # Prints True
indicates that both initialised empty struct has the same address. Are empty structs automatically "singleton"? Or is it a bug in the compiler? I am using Mac M1.
7 replies
MModular
Created by ivellapillil on 5/15/2024 in #community-showcase
Online book on Mojo
35 replies