Lambda Function parameter infer type - External Struct #290
Replies: 3 comments 6 replies
-
Inferring type from a lambda expression's Struct parameterHey Applied to your use-casevector::for_each<Token>(nfts, |nft| {
let id = token::get_token_id(&nft);
vector::push_back<TokenId>(&mut trackUserAssets.tokens, id);
// ......
} |
Beta Was this translation helpful? Give feedback.
-
Yes that works thanks a lot! |
Beta Was this translation helpful? Give feedback.
-
but now if I have this: vector::for_each<NFT>(nfts, |nft| {
let token_addr = object::object_address(&nft.token);
let token = borrow_global<Token>(token_addr);
let id = get_token_id(token);
vector::push_back<TokenId>(&mut trackUserAssets.tokens, id);
deposit_token(account, nft);
}); I get this error: error[E04007]: incompatible types
┌─ /home/spielcrypto/ghost-lost/ghost-lost/ghostly-contract/sources/ghostly.move:25:9
│
25 │ ╭ vector::for_each<NFT>(nfts, |nft| {
│ --- Given: '(ghostly=0x1)::keep::NFT'
26 │ │ let token_addr = object::object_address(&nft.token);
27 │ │ let token = borrow_global<Token>(token_addr);
28 │ │ let id = get_token_id(token);
· │
31 │ │ deposit_token(account, nft);
32 │ │ });
│ ╰──────────^ Invalid call of '(std=0x1)::vector::for_each'. Invalid argument for parameter 'f'
│
┌─ /home/spielcrypto/.move/https___github_com_aptos-labs_aptos-core_git_testnet/aptos-move/framework/aptos-token/sources/token.move:955:55
│
955 │ public fun deposit_token(account: &signer, token: Token) acquires TokenStore {
│ ----- Expected: '(aptos_token=0x3)::token::Token'
error[E04010]: cannot infer type
┌─ /home/spielcrypto/ghost-lost/ghost-lost/ghostly-contract/sources/ghostly.move:26:30
│
26 │ let token_addr = object::object_address(&nft.token);
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Cannot infer the type parameter `T` for generic function `(aptos_framework=0x1)::object::object_address`. Try providing a type parameter.
error[E04010]: cannot infer type
┌─ /home/spielcrypto/ghost-lost/ghost-lost/ghostly-contract/sources/ghostly.move:26:54
│
25 │ vector::for_each<NFT>(nfts, |nft| {
│ --- Could not infer the type before field access. Try annotating here
26 │ let token_addr = object::object_address(&nft.token);
│ ^^^^^^^^^ Unbound field 'token'
error[E05001]: ability constraint not satisfied
┌─ /home/spielcrypto/ghost-lost/ghost-lost/ghostly-contract/sources/ghostly.move:27:25
│
27 │ let token = borrow_global<Token>(token_addr);
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
│ │ │
│ │ The type '(aptos_token=0x3)::token::Token' does not have the ability 'key'
│ Invalid call of 'borrow_global'
│
┌─ /home/spielcrypto/.move/https___github_com_aptos-labs_aptos-core_git_testnet/aptos-move/framework/aptos-token/sources/token.move:158:12
│
158 │ struct Token has store {
│ ----- To satisfy the constraint, the 'key' ability would need to be added here
error[E04001]: restricted visibility
┌─ /home/spielcrypto/ghost-lost/ghost-lost/ghostly-contract/sources/ghostly.move:27:25
│
27 │ let token = borrow_global<Token>(token_addr);
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
│ │ │
│ │ The type '(aptos_token=0x3)::token::Token' was not declared in the current module. Global storage access is internal to the module'
│ Invalid call to borrow_global. The NFT struct is this: struct NFT has key, copy, drop {
token: Object<Token>,
collection: Object<Collection>,
} It is like inside for_each I cannot get any prop from any |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Discord user ID
spielcrypto.apt
Describe your question in detail.
My
Token
Struct parameter can't have it'sid
field accessed.vector::for_each
's lambda parameter's type can not be inferred.What error, if any, are you getting?
What have you tried or looked at? Or how can we reproduce the error?
No response
Which operating system are you using?
N/A
Which SDK or tool are you using? (if any)
Aptos CLI
Describe your environment or tooling in detail
No response
Beta Was this translation helpful? Give feedback.
All reactions