Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Borrow function in NFT Docs #895

Merged
merged 2 commits into from
Sep 19, 2024
Merged

Conversation

DappCoderr
Copy link
Contributor

In the Collection resource borrowNFT_ function gives an error.
NonFungibleToken type cast to &{NFT}? is redundant.

Solution: To remove this error add Force Unwrap Operator (!).

In function borrowNFT_  NonFungibleToken type cast to `&{NFT}?` is redundant. To remove this error added Force Unwrap Operator (!).
Copy link

vercel bot commented Sep 11, 2024

@DappCoderr is attempting to deploy a commit to the Flow Team on Vercel.

A member of the Team first needs to authorize it.

Copy link

vercel bot commented Sep 11, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 19, 2024 2:40pm

Copy link
Contributor

@sisyphusSmiling sisyphusSmiling left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks for the correction!

Copy link
Member

@joshuahannan joshuahannan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is correct. This function is supposed to return an optional, so we should not be force unwrapping it here. See ExampleNFT for the correct implementation.

@DappCoderr Can you explain what you're trying to do here? Maybe I'm not understanding

@DappCoderr
Copy link
Contributor Author

I don't think this is correct. This function is supposed to return an optional, so we should not be force unwrapping it here. See ExampleNFT for the correct implementation.

@DappCoderr Can you explain what you're trying to do here? Maybe I'm not understanding

I understand that the value should be returned as optional. However, when I use it, I encounter the compilation error "cast to &{NFT}? is redundant."

image

To resolve this, I've added the force unwrap operator (!).

image

@joshuahannan
Copy link
Member

That isn't an error, it is just a warning, so the contract will still deploy if you ignore it.

That is just pointing out that the entire cast is redundant, not the ?, if you just remove the cast completely, it will work properly.

        access(all) view fun borrowNFT(_ id: UInt64): &{NonFungibleToken.NFT}? {
            return &self.ownedNFTs[id]
        }

Can you update the PR to just have that? Thank you!

@DappCoderr
Copy link
Contributor Author

Thanks for the clarification, @joshuahannan
I'll remove the redundant cast and push the updated PR.

Removing the cast to &{NonFungibleToken.NFT} as it's redundant and introduces potential warnings.
@joshuahannan joshuahannan merged commit 2e27326 into onflow:main Sep 19, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants