Hello!
I’m new to Mojo and am working with a custom Point struct in Mojo and found that destructuring assignment only works when the struct conforms to ImplicitlyCopyable, not just Copyable.
Here’s my struct:
@fieldwise_init
struct Point[dtype: DType](Copyable, Writable) where dtype.is_numeric():
comptime _coord_type = Scalar[Self.dtype]
var x: Self._coord_type
var y: Self._coord_type
With just Copyable, this doesn’t work:
comptime point_type = Point[dtype=DType.float64]
var p: point_type
var p1: point_type
var p2: point_type
p, p1, p2 = point_type(10, 0), point_type(0, 100), point_type(0, 1)
And this works:
p = point_type(10, 0)
p1 = point_type(0, 100)
p2 = point_type(0, 1)
But when I change it to ImplicitlyCopyable, destructuring assignment works as expected.
My question:
What’s the fundamental difference between Copyable and ImplicitlyCopyable that makes destructuring assignment require the latter?
I understand that Copyable allows explicit copying, but why does destructuring assignment specifically need implicit copying behavior? Is there a technical reason related to how the compiler handles the unpacking operation?
I’d like to understand the rationale behind this requirement to better know when to use each trait.
Thanks!
EDIT: I’m using Mojo version 1.2.0.