What can’t it take immutable ref?
public fun exists_with_type<Name: copy, drop, store, Value: store, key>(object: &object::UID, name: Name): bool
What can’t it take immutable ref?
public fun exists_with_type<Name: copy, drop, store, Value: store, key>(object: &object::UID, name: Name): bool
@shiqicao - I am not a Move expert, but I believe the answer lies in the fact that references cannot appear in global storage, hence they do not have store
. https://github.com/move-language/move/blob/main/language/documentation/book/src/abilities.md#builtin-types
I will double check with someone from the Move team and confirm if this is indeed the reason.
I am not suggesting store a ref. exist_
function doesn’t store any value? Similar to Rust, HashMap::contains_key<Q>(&self, k: &Q) -> bool
takes a ref.
Theoretically it could, and especially so for exists_
, but we have to keep many things by-value to avoid potential borrow checker issues. For example with
fun borrow<Name: copy + drop + store, Value: store>(
object: &UID,
name: Name,
): &Value
If we had instead name: &Name
, then the resulting &Value
would also borrow from name
.
In Rust, you can get around these issues with lifetime annotations, but that is something Move does not support.
So as a result, we require copy
across the board for all of the keys. And at that point there isn’t much need for the reference since you can always copy the value. (Keep in mind that even if it was by-reference there wouldn’t be much of a gas difference since it would still need to copy out the underlying bytes of the value to generate the hash for the dynamic field)
this is a general problem needs to be solved in Move!