You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to write a generic function in Move that works with multiple structs having a common field. Here's the example:
structTypeAhasstore, copy, drop {
id: u128,
value: u64,
}
structTypeBhasstore, copy, drop {
id: u128,
value: u64,
}
publicfunfind_item_index<T>(
items: &vector<T>, target_id: u128
): (bool, u64) {
let length = vector::length(items);
let found = false;
let index = 0;
let i = 0;
while (i < length) {
// Error occurs hereif (items.borrow(i).id == target_id) {
found = true;
index = i;
break;
};
i = i + 1;
};
(found, index)
}
The issue I'm facing is that the compiler gives an error: "Unresolved field: id" when trying to access the id field of items.borrow(i).
How can I make the generic type T in find_item_index ensure that it has an id field? Is there a way to define constraints for generic types in Move to solve this issue?
What error, if any, are you getting?
"Unresolved field: id" when trying to access the id field.
What have you tried or looked at? Or how can we reproduce the error?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Discord user ID
0xanto_
Describe your question in detail.
I'm trying to write a generic function in Move that works with multiple structs having a common field. Here's the example:
The issue I'm facing is that the compiler gives an error: "Unresolved field:
id
" when trying to access theid
field ofitems.borrow(i)
.How can I make the generic type
T
infind_item_index
ensure that it has anid
field? Is there a way to define constraints for generic types in Move to solve this issue?What error, if any, are you getting?
"Unresolved field:
id
" when trying to access theid
field.What have you tried or looked at? Or how can we reproduce the error?
No response
Which operating system are you using?
macOS
Which SDK or tool are you using? (if any)
Aptos CLI
Describe your environment or tooling in detail
aptos 5.1.0
Beta Was this translation helpful? Give feedback.
All reactions