@value decorator
generates constructors whose arguments are owned
#392
-
The @value
struct MyPet:
var name: String
var age: Int
fn __init__(inout self, owned name: String, age: Int):
self.name = name^
self.age = age
fn __copyinit__(inout self, existing: Self):
self.name = existing.name
self.age = existing.age
fn __moveinit__(inout self, owned existing: Self):
self.name = existing.name^
self.age = existing.age I am having trouble figuring out why |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Sure, no problem. It passes /all/ of the arguments as owned. The reason Int isn't passed as owned is just because it doesn't matter either way - Int is a register_passable(trivial) type, and trivial types have no ownership. I added this statement to clarify, thanks! The arguments to |
Beta Was this translation helpful? Give feedback.
Sure, no problem. It passes /all/ of the arguments as owned. The reason Int isn't passed as owned is just because it doesn't matter either way - Int is a register_passable(trivial) type, and trivial types have no ownership. I added this statement to clarify, thanks!
The arguments to
__init__
are all passed asowned
arguments since the structtakes ownership and stores the value. This is a useful micro-optimization and
enables the use of move-only types. Trivial types like
Int
are also passedas owned values, but since that doesn't mean anything for them, we elide the
marker and the transfer operator (
^
) for clarity.