struct A
a::Int
b::Int
end
a, b = A(10, 20)
I found an anwer before, but I can’t find it anymore.
How can I do it?
struct A
a::Int
b::Int
end
a, b = A(10, 20)
I found an anwer before, but I can’t find it anymore.
How can I do it?
(;a,b) =
Thanks for your replly. I think it is a little of messy. Is there a more brief way?
like:
Base.iterate(x::A, i::Int) = iterate((x.a, x.b), i)
You can do
Base.iterate(x::A, i = 1) = (i <= fieldcount(A)) ? (getfield(x, i), i + 1) : nothing
but I wouldn’t really recommend it. The struct destructuring syntax is based on names instead of order and thus allows extracting arbitrary subsets of the fields and is insensitive to reordering of the fields.