Downcasting from AnyResource?
Trying to figure out a way to allow for generic attachments to NFTs. Does anyone know if we can downcast from
&ref as auth &AnyResource
?
Something like
2 Replies
Yes, that is possible, as the reference is an
auth
orized reference, which makes it downcastable. You just need to use a conditional cast (as?
) or force-cast (as!
), instead of a type assertion (as
). as
is a static check – statically, &AnyResource
is not a subtype of &MyType
. What you want to do is ref
and try to cast it at run-time. Here's an example Playground: https://play.flow.com/83473238-9b05-4132-8464-ef210e300e0d?type=script&id=26f962de-3256-434f-8300-b28e6eccb190&storage=noneTry out this Playground project
Learn Cadence, a revolutionary new smart contract programming language written for the Flow blockchain.
Very helpful, thanks for the example!