F
Flow4mo ago
joshua

joshua | Flow (2024-02-08)

Is it possible to borrow a contract as one of its interface types if I know the contract's name and address? like this:
// Borrow a reference to the contract as an interface type
let fungibleRef = getAccount(contractAddress)
.contracts.borrow<&{FungibleToken}>(name: contractName)
?? panic("Could not borrow a reference to the fungible token contract")
// Borrow a reference to the contract as an interface type
let fungibleRef = getAccount(contractAddress)
.contracts.borrow<&{FungibleToken}>(name: contractName)
?? panic("Could not borrow a reference to the fungible token contract")
I tried it and I am getting an error:
error: cannot restrict using non-resource/structure interface type: `FungibleToken`
--> 8a7abd15e963aff4f5c29b736ebeedac21bdbd34f5203efdecab52d7776d0570:15:32
|
15 | .contracts.borrow<&{FungibleToken}>(name: contractName)
| ^^^^^^^^^^^^^

error: ambiguous intersection type
--> 8a7abd15e963aff4f5c29b736ebeedac21bdbd34f5203efdecab52d7776d0570:15:31
|
15 | .contracts.borrow<&{FungibleToken}>(name: contractName)
|
error: cannot restrict using non-resource/structure interface type: `FungibleToken`
--> 8a7abd15e963aff4f5c29b736ebeedac21bdbd34f5203efdecab52d7776d0570:15:32
|
15 | .contracts.borrow<&{FungibleToken}>(name: contractName)
| ^^^^^^^^^^^^^

error: ambiguous intersection type
--> 8a7abd15e963aff4f5c29b736ebeedac21bdbd34f5203efdecab52d7776d0570:15:31
|
15 | .contracts.borrow<&{FungibleToken}>(name: contractName)
|
6 Replies
Needle
Needle4mo ago
I've created a thread for your message. Please continue any relevant discussion in this thread. You can rename this thread using /title <new title> If this is a technical question that others may benefit from, considering also asking it on Stackoverflow: https://stackoverflow.com/questions/ask?tags=onflow-cadence
Unknown User
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View
joshua
joshua4mo ago
thanks! I assumed contract interfaces had the same syntax as resource interfaces. seems weird that they don't
turbolent
turbolent4mo ago
yeah, that's not really how it's supposed to work. hmm
Unknown User
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View
turbolent
turbolent4mo ago
the "cannot restrict using non-resource/structure interface type" error is just an unnecessarily restrictive check in the type checker, not sure if 1.0 is better there https://github.com/onflow/cadence/issues/3088 https://github.com/onflow/cadence/pull/3089 https://github.com/onflow/cadence/pull/3090