Reference types in Cadence

100 views Asked by At

I have been messing around with types, specifically reference types, using the flow playground ft tutorial and I came up with a doubt. References are supposed to have their own type, a reference to the referenced resource type; e.g for a @Collectible resource whose type will be <A.0...01.Collectible>() a reference to that resource &Collectible I was expecting something like <&A.0..01.Collectible>() but when calling the getType() function on a reference I get the same type that I get when calling said function on the referenced resource.

To clarify, this code... (that can be found on the line 32 of the transaction "Transfer tokens" on the link above)

log(self.temporaryVault.getType())

log(receiverRef.getType())

log(self.temporaryVault.getType() == receiverRef.getType())

...I get this output

  • Type<A.0000000000000002.ExampleToken.Vault>()
  • Type<A.0000000000000002.ExampleToken.Vault>()
  • true

What I'm missing here?

2

There are 2 answers

0
turbolent On BEST ANSWER

Accesses on references always automatically dereference the reference – operations are performed on the referenced value, not the reference.

Accesses are e.g.:

  • Member accesses, accessing fields and functions, like ref.field or ref.func()
  • Indexing accesses, like ref[0]
0
Deniz Mert Edincik On

This one is little hard to explain, but in my understanding: references are just proxies to the object.

So when you actually call getType(), you are actually calling it on the referenced object.