cadence icon indicating copy to clipboard operation
cadence copied to clipboard

An Unexpected "Unexpected Token in Expression" Error

Open orodio opened this issue 3 years ago • 2 comments

I wrote a script that fetches the balance for FUSD on testnet. I was expecting the following to work.

import FungibleToken from 0x9a0766d93b6608b7

pub fun main(addr: Address): UFix64 {
  return getAccount(addr)
    .getCapability<&{FungibleToken.Balance}>(/public/fusdBalance)
    .borrow()?
    .balance ?? 0.0
}

Because of the following error i was able to narrow it down to the following two lines:

    .borrow()?
    .balance ?? 0.0

The error:

Error: 2 errors occurred:
	* rpc error: code = Internal desc = failed to execute the script on the execution node execution-a1f059a3e00347e585658e4c400be1580ba2e2f6d1f39387ad62e33f3f4d246d@execution-002.devnet24.nodes.onflow.org:3569=1000: rpc error: code = Internal desc = failed to execute script: failed to execute script at block (199acc3dc3c330678defd49109e89d33b3803f2d4f939178200a27187b3c8453): [Error Code: 1101] cadence runtime error Execution failed:
error: unexpected token in expression: '.'
 --> f68db72b0ac3834bb7e154b5ff1aca22d8eff14ebe12403cdb4f5f4b79ee75de:8:15
  |
8 |               .balance ?? 0.0
  |                ^

	* rpc error: code = Internal desc = failed to execute the script on the execution node execution-5f6c73a22445d7d958c6a37c1f3be99c72cacd39894a3e46d6647a9adb007b4d@execution-001.devnet24.nodes.onflow.org:3569=1000: rpc error: code = Internal desc = failed to execute script: failed to execute script at block (199acc3dc3c330678defd49109e89d33b3803f2d4f939178200a27187b3c8453): [Error Code: 1101] cadence runtime error Execution failed:
error: unexpected token in expression: '.'
 --> f68db72b0ac3834bb7e154b5ff1aca22d8eff14ebe12403cdb4f5f4b79ee75de:8:15
  |
8 |               .balance ?? 0.0
  |                ^

It seems it did not like the whitespace bewtween the ? and the .. Looking into it more I have found that the following work:

/* A */    .borrow()?.balance ?? 0.0
/* B */    .borrow() ?.balance ?? 0.0
/* C */    .borrow()
           ?.balance ?? 0.0

But the following do not work:

/* D */ .borrow()?. balance ?? 0.0
/* E */ .borrow()? .balance ?? 0.0
/* F */ .borrow() ?. balance ?? 0.0
/* G */ .borrow()?.
        balance ?? 0.0
/* H */ .borrow()?
        .balance ?? 0.0

I was lead to believe that the way I wrote it initially would work because the following works as expected.

import FungibleToken from 0x9a0766d93b6608b7

pub fun main(addr: Address): UFix64 {
  return getAccount(addr)
    .getCapability<&{FungibleToken.Balance}>(/public/fusdBalance)
    .borrow()!
    .balance
}

I don't think this is particularly urgent. Also don't know if this is a regression or something else. More just wanted to bring it to your attention that something doesn't line up.

Personally I think about the ? being part of .borrow() not part of balance, even though Im sure in actuality it isn't part of either and ?. is an operator or something but then I probably would have expected .borrow() ?. balance to work :P but still mentally I think of if as .borrow()? and .balance. /shrug

orodio avatar Jul 05 '21 18:07 orodio