Skip to content

Instantly share code, notes, and snippets.

@modularbot
Created April 26, 2024 05:54
Show Gist options
  • Save modularbot/5adaa5b685efe9f99ba973d9e8811ed1 to your computer and use it in GitHub Desktop.
Save modularbot/5adaa5b685efe9f99ba973d9e8811ed1 to your computer and use it in GitHub Desktop.
struct DemoStruct(CollectionElement, Stringable):
var member1: Int
fn __init__(inout self, member1: Int) -> None:
self.member1 = member1
print("Called constructor for " + str(self))
fn __copyinit__(inout self, other: Self) -> None:
self.member1 = other.member1
print("Called copy constructor for " + str(self))
fn __moveinit__(inout self, owned other: Self) -> None:
self.member1 = other.member1
print("Called move constructor for " + str(self))
fn __del__(owned self) -> None:
print("Calling destructor for " + str(self))
fn __str__(self) -> String:
return "DemoStruct(" + str(self.member1) + ")"
fn display(owned self) -> Self:
print("Entering display function")
print(str(self))
print("Exiting display function")
return self
fn main() -> None:
var demo_obj_1 = DemoStruct(42).display().display()
# __moveinit__ is called twice for the above line
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment