Skip to content

Instantly share code, notes, and snippets.

@modularbot
Created April 4, 2024 02:20
Show Gist options
  • Save modularbot/0613c95485ee838e00dc7289b81efa2c to your computer and use it in GitHub Desktop.
Save modularbot/0613c95485ee838e00dc7289b81efa2c to your computer and use it in GitHub Desktop.
alias dtype = DType.float64
struct MyStruct:
var ptr : DTypePointer[dtype]
var len:Int
fn __init__(inout self, len:Int):
self.len = len
self.ptr = DTypePointer[dtype].alloc(len)
fn __copyinit__(inout self, borrowed existing:Self):
print("> __copyinit__()")
self.len = existing.len
self.ptr = DTypePointer[dtype].alloc(self.len)
for i in range(self.len):
self.ptr[i] = existing.ptr[i]
fn __moveinit__(inout self, owned existing:Self):
print("> __moveinit__()")
self.len = existing.len
self.ptr = existing.ptr
fn __del__(owned self):
print("> __del__()")
self.ptr.free()
fn do_something_meaningful(owned val:Float64):
print("amount donated to Modular:", val,"$")
fn main():
print("$ var a = MyStruct(10)")
var a = MyStruct(10)
print("$ a.ptr[0] = 37")
a.ptr[0] = 37
print("$ do_something_meaningful(a.ptr[0])")
do_something_meaningful(a.ptr[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment