-
-
Save modularbot/8409f3e22e7da946d2e84c798c27f40e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 modify(owned self, new_value: Int) -> Self: | |
print("Entering modify function") | |
self.member1 = new_value | |
print("Exiting modify function") | |
return self | |
fn main() -> None: | |
var demo_obj_1 = DemoStruct(42).modify(10).modify(20) | |
# __moveinit__ is called twice for the above line | |
print(demo_obj_1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment