39 lines
1002 B
Mojo
39 lines
1002 B
Mojo
# probe: copy and move constructors through var arguments and transfers
|
|
# status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt
|
|
# expect-stdout: new a
|
|
# expect-stdout: copy a
|
|
# expect-stdout: consumed a+
|
|
# expect-stdout: free a+
|
|
# expect-stdout: move a
|
|
# expect-stdout: holding a
|
|
# expect-stdout: free a
|
|
struct Box(Copyable, Movable):
|
|
var label: String
|
|
|
|
def __init__(out self, label: String):
|
|
self.label = label
|
|
print("new", label)
|
|
|
|
def __init__(out self, *, copy: Self):
|
|
self.label = copy.label + "+"
|
|
print("copy", copy.label)
|
|
|
|
def __init__(out self, *, deinit move: Self):
|
|
self.label = move.label^
|
|
print("move", self.label)
|
|
|
|
def __deinit__(deinit self):
|
|
print("free", self.label)
|
|
|
|
|
|
def consume(var b: Box):
|
|
print("consumed", b.label)
|
|
|
|
|
|
def main():
|
|
var a = Box("a")
|
|
var b = a.copy()
|
|
consume(b^)
|
|
var c = a^
|
|
print("holding", c.label)
|