Mismatched types. expected `Never`, got `Int`

Hi, I am trying to modify mintNFT to takes number as argument so that it can mint for multiple items.

like this.

        pub fun mintNFT(num: Int, recipient: &AnyResource{NFTReceiver}): [Int] {
            var a = 0
            var ids = []
            while a < num {
                // create a new NFT
                var newNFT <- create NFT(initID: self.idCount)
                // deposit it in the recipient's account using their reference
                recipient.deposit(token: <-newNFT)

                // change the id so that each ID is unique
                self.idCount = self.idCount + UInt64(1)
                ids[a] = Int(self.idCount)
                a = a + 1
            }
            return ids
        }

When I try to return the generated idCount as a array, I am getting the mismatched types error at Int(self.idCount).

Any idea how to work around?

tagging @flowjosh

@makoto I think you need to specify the type of ids in line 116

var ids: [Int] = []

I changed var ids = [] to var ids: [Int] = [] and now VS code shows no error and I can deploy.
However, running transaction kills the emulator with bunch of errors.

here is the transaction code

// Transaction2.cdc

import NonFungibleToken from 0x01cf0e2f2f715450

// This transaction allows the Minter account to mint an NFT
// and deposit it into its collection.

transaction {

    // The reference to the collection that will be receiving the NFT
    let receiverRef: &{NonFungibleToken.NFTReceiver}

    // The reference to the Minter resource stored in account storage
    let minterRef: &NonFungibleToken.NFTMinter

    // let collectionRef: &NonFungibleToken.Collection
    prepare(acct: AuthAccount) {
        // Get the owner's collection capability and borrow a reference
        self.receiverRef = acct.getCapability(/public/NFTReceiver)!
                               .borrow<&{NonFungibleToken.NFTReceiver}>()!
        
        // Borrow a capability for the NFTMinter in storage
        self.minterRef = acct.borrow<&NonFungibleToken.NFTMinter>(from: /storage/NFTMinter)!
        // self.collectionRef = acct.borrow<&NonFungibleToken.Collection>(from: /storage/NFTCollection)!
    }

    execute {
        // Use the minter reference to mint an NFT, which deposits
        // the NFT into the collection that is sent as a parameter.
        self.minterRef.mintNFT(num: 3, recipient: self.receiverRef)

        log("NFT Minted and deposited to Account 2's Collection")

        log("Account 2 NFTs")

        log(self.receiverRef.getIDs())

        // Call the withdraw function on the sender's Collection
        // to move the NFT out of the collection
        // let transferToken <- self.collectionRef.withdraw(withdrawID: 1)

    }
}

and
error on the terminal

DEBU[0003] πŸ“¦  Block #2 committed                         blockHeight=2 blockID=cd1fe0fc92af1997f73efef871c753bb71e470cdb689a3bf2b4cc6d377e9ac98
DEBU[0003] πŸ“  GetTransactionResult called                txID=164ea8fdb4fcb75e40ee8798cc79716fbd58b09ee7cd6af1530103ccf3e0f639
DEBU[0007] πŸ‘€  GetAccount called                          address=01cf0e2f2f715450
DEBU[0045] πŸ‘€  GetAccount called                          address=01cf0e2f2f715450
DEBU[0046] οΈβœ‰οΈ   Transaction submitted                   txID=aaaefe3501b91ffef66038f0b70c3efb6bd4f1fc845ef28df4d4cb4791425f0b
INFO[0046] ⭐  Transaction executed                       txID=aaaefe3501b91ffef66038f0b70c3efb6bd4f1fc845ef28df4d4cb4791425f0b
DEBU[0046] EVT [aaaefe] flow.AccountCodeUpdated: 24c7063d9d6bbcaa1e1631cbd3d9f0e0a19c54c877bdeda53c4b838d21f81f79 
DEBU[0046] πŸ“¦  Block #3 committed                         blockHeight=3 blockID=f5c3b4389d64313e9dc5712deb8071a8a0f6cc2715500e1a2751b3c8bd3429d0
DEBU[0082] πŸ‘€  GetAccount called                          address=01cf0e2f2f715450
DEBU[0082] οΈβœ‰οΈ   Transaction submitted                   txID=b485fb0c5fb4ce82d8429202bdf36f2c6b55e1c61f602e7218493d9ec7f79be4
panic: runtime error: index out of range [0] with length 0 [recovered]
        panic: runtime error: index out of range [0] with length 0

goroutine 115 [running]:
github.com/onflow/cadence/runtime/interpreter.recoverErrors(0xc00085a1f8)
        github.com/onflow/cadence@v0.5.1/runtime/interpreter/interpreter.go:864 +0x15a
panic(0x4e0ef40, 0xc0003e34a0)
        runtime/panic.go:969 +0x166
github.com/onflow/cadence/runtime/interpreter.(*ArrayValue).SetIndex(...)
        github.com/onflow/cadence@v0.5.1/runtime/interpreter/value.go:544
github.com/onflow/cadence/runtime/interpreter.(*ArrayValue).Set(0xc000771200, 0xc000396780, 0x51d2900, 0xc0003b9de0, 0x1027, 0x7d, 0x13, 0x1029, 0x7d, 0x15, ...)
        github.com/onflow/cadence@v0.5.1/runtime/interpreter/value.go:538 +0x100
github.com/onflow/cadence/runtime/interpreter.(*Interpreter).indexExpressionGetterSetter.func1.1.2(0x51fe9e0, 0xc0002524c0)
        github.com/onflow/cadence@v0.5.1/runtime/interpreter/interpreter.go:1461 +0xd4
github.com/onflow/cadence/runtime/interpreter.(*Interpreter).visitAssignment.func1.1(0x4e956a0, 0xc000383fc0, 0xc000383fc0, 0xc000252360)
        github.com/onflow/cadence@v0.5.1/runtime/interpreter/interpreter.go:1366 +0xf6
github.com/onflow/cadence/runtime/trampoline.FlatMap.FlatMap.func1(0x4e956a0, 0xc000383fc0, 0x4cad720, 0xc0002523a0)
        github.com/onflow/cadence@v0.5.1/runtime/trampoline/trampoline.go:142 +0x42
github.com/onflow/cadence/runtime/trampoline.FlatMap.Resume.func1(0xc000252360, 0x4cad720)
        github.com/onflow/cadence@v0.5.1/runtime/trampoline/trampoline.go:154 +0x37
github.com/onflow/cadence/runtime/interpreter.(*Interpreter).runUntilNextStatement(0xc0003966c0, 0x51ee720, 0xc000252360, 0x0, 0x0, 0xc0003835c0)
        github.com/onflow/cadence@v0.5.1/runtime/interpreter/interpreter.go:608 +0x35
github.com/onflow/cadence/runtime/interpreter.(*Interpreter).runAllStatements(0xc0003966c0, 0x51ee720, 0xc0000184c0, 0x1, 0x1)
        github.com/onflow/cadence@v0.5.1/runtime/interpreter/interpreter.go:620 +0x56
github.com/onflow/cadence/runtime/interpreter.(*Interpreter).InvokeTransaction(0xc0003966c0, 0x0, 0xc00049a1c0, 0x1, 0x1, 0x0, 0x0)
        github.com/onflow/cadence@v0.5.1/runtime/interpreter/interpreter.go:853 +0x118
github.com/onflow/cadence/runtime.(*interpreterRuntime).transactionExecutionFunction.func1(0xc0003966c0, 0x0, 0x0, 0x507b0700085a338, 0x5f10922a)
        github.com/onflow/cadence@v0.5.1/runtime/runtime.go:400 +0x183
github.com/onflow/cadence/runtime.(*interpreterRuntime).interpret.func1()
        github.com/onflow/cadence@v0.5.1/runtime/runtime.go:232 +0xa4
github.com/onflow/cadence/runtime.reportMetric(0xc00085a460, 0x52134c0, 0xc0005b6500, 0xc00085a448)
        github.com/onflow/cadence@v0.5.1/runtime/runtime.go:104 +0x93
github.com/onflow/cadence/runtime.(*interpreterRuntime).interpret(0xc0000a652a, 0x51d28e0, 0xc0003b9c60, 0x52134c0, 0xc0005b6500, 0xc0003b9ca0, 0xc000130200, 0xc0007f3180, 0x8, 0xc, ...)
        github.com/onflow/cadence@v0.5.1/runtime/runtime.go:226 +0x1d2
github.com/onflow/cadence/runtime.(*interpreterRuntime).ExecuteTransaction(0xc0000a652a, 0xc0001c3c00, 0x618, 0x700, 0x0, 0x0, 0x0, 0x52134c0, 0xc0005b6500, 0x51d28e0, ...)
        github.com/onflow/cadence@v0.5.1/runtime/runtime.go:339 +0x57d
github.com/dapperlabs/flow-go/fvm.(*TransactionInvocator).Process(0x6f31c78, 0xc00040f510, 0x51fbaa0, 0x6eea800, 0x51dc820, 0xc0003903a0, 0x51d26e0, 0xc000336b40, 0x0, 0x186a0, ...)
        github.com/dapperlabs/flow-go@v0.4.1-0.20200710172932-ed59eb7cd8d1/fvm/transaction.go:90 +0x272
github.com/dapperlabs/flow-go/fvm.(*TransactionProcedure).Run(0xc000180fc0, 0xc00040f510, 0x51fbaa0, 0x6eea800, 0x51dc820, 0xc0003903a0, 0x51d26e0, 0xc000336b40, 0x0, 0x186a0, ...)
        github.com/dapperlabs/flow-go@v0.4.1-0.20200710172932-ed59eb7cd8d1/fvm/transaction.go:37 +0xe4
github.com/dapperlabs/flow-go/fvm.(*VirtualMachine).Run(...)
        github.com/dapperlabs/flow-go@v0.4.1-0.20200710172932-ed59eb7cd8d1/fvm/fvm.go:29
github.com/dapperlabs/flow-emulator.(*Blockchain).executeNextTransaction.func1(0xc0007b9740, 0xc000397080, 0xc0006a8d18, 0xc0004ded68, 0x100000004551663)
        github.com/dapperlabs/flow-emulator@v0.5.1/blockchain.go:613 +0x1c7
github.com/dapperlabs/flow-emulator.(*pendingBlock).ExecuteNextTransaction(0xc000387680, 0xc0006a8f90, 0x51d26e0, 0xc000336b40, 0x0)
        github.com/dapperlabs/flow-emulator@v0.5.1/pendingBlock.go:148 +0x10f
github.com/dapperlabs/flow-emulator.(*Blockchain).executeNextTransaction(0xc000336b40, 0x51fbaa0, 0x6eea800, 0x51dc820, 0xc0003903a0, 0x51d26e0, 0xc000336b40, 0x0, 0x186a0, 0xc00017cc40, ...)
        github.com/dapperlabs/flow-emulator@v0.5.1/blockchain.go:606 +0xa7
github.com/dapperlabs/flow-emulator.(*Blockchain).executeBlock(0xc000336b40, 0x0, 0x194, 0x100000521b4e0, 0x1, 0x0)
        github.com/dapperlabs/flow-emulator@v0.5.1/blockchain.go:570 +0x20e
github.com/dapperlabs/flow-emulator.(*Blockchain).executeAndCommitBlock(0xc000336b40, 0xc0005b96c0, 0x88, 0xc0006a9540, 0x423eb26, 0xc0005b96c0, 0xc0005b97b0)
        github.com/dapperlabs/flow-emulator@v0.5.1/blockchain.go:694 +0x43
github.com/dapperlabs/flow-emulator.(*Blockchain).ExecuteAndCommitBlock(0xc000336b40, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
        github.com/dapperlabs/flow-emulator@v0.5.1/blockchain.go:688 +0x95
github.com/dapperlabs/flow-emulator/server.(*Backend).commitBlock(0xc000361140)
        github.com/dapperlabs/flow-emulator@v0.5.1/server/backend.go:428 +0x4c
github.com/dapperlabs/flow-emulator/server.(*Backend).SendTransaction(0xc000361140, 0x51ed6e0, 0xc00048bb30, 0xc00048bb60, 0xc000361140, 0x7e89960, 0xc0000c6960)
        github.com/dapperlabs/flow-emulator@v0.5.1/server/backend.go:86 +0x8e5
github.com/onflow/flow/protobuf/go/flow/access._AccessAPI_SendTransaction_Handler.func1(0x51ed6e0, 0xc00048bb30, 0x4e120e0, 0xc00048bb60, 0x21, 0xc000362dc0, 0x0, 0xc0006a9b30)
        github.com/onflow/flow/protobuf/go/flow@v0.1.5-0.20200611205353-548107cc9aca/access/access.pb.go:1825 +0x89
github.com/grpc-ecosystem/go-grpc-prometheus.(*ServerMetrics).UnaryServerInterceptor.func1(0x51ed6e0, 0xc00048bb30, 0x4e120e0, 0xc00048bb60, 0xc000383d60, 0xc000383d80, 0xc000574ba0, 0x410ee08, 0x4e01640, 0xc00048bb30)
        github.com/grpc-ecosystem/go-grpc-prometheus@v1.2.0/server_metrics.go:107 +0xad
github.com/onflow/flow/protobuf/go/flow/access._AccessAPI_SendTransaction_Handler(0x4e851a0, 0xc000361140, 0x51ed6e0, 0xc00048bb30, 0xc0003bd860, 0xc000353810, 0x51ed6e0, 0xc00048bb30, 0xc0001c3500, 0x6b0)
        github.com/onflow/flow/protobuf/go/flow@v0.1.5-0.20200611205353-548107cc9aca/access/access.pb.go:1827 +0x14b
google.golang.org/grpc.(*Server).processUnaryRPC(0xc0005b8a80, 0x51ffca0, 0xc00009aa80, 0xc0005b6600, 0xc0003b2750, 0x6ef4b00, 0x0, 0x0, 0x0)
        google.golang.org/grpc@v1.30.0/server.go:1171 +0x50a
google.golang.org/grpc.(*Server).handleStream(0xc0005b8a80, 0x51ffca0, 0xc00009aa80, 0xc0005b6600, 0x0)
        google.golang.org/grpc@v1.30.0/server.go:1494 +0xccb
google.golang.org/grpc.(*Server).serveStreams.func1.2(0xc0000458a0, 0xc0005b8a80, 0x51ffca0, 0xc00009aa80, 0xc0005b6600)
        google.golang.org/grpc@v1.30.0/server.go:834 +0xa1
created by google.golang.org/grpc.(*Server).serveStreams.func1
        google.golang.org/grpc@v1.30.0/server.go:832 +0x204

And error on OUTPUT

[Info  - 18:43:46] Successfully loaded config emu_addr: localhost:3569
DidOpenTextDoc
[Info  - 18:43:46] parsing file:///Users/makoto/work/tmp/flow/week2/Transaction2.cdc took 199.863Β΅s
[Info  - 18:43:46] checking file:///Users/makoto/work/tmp/flow/week2/Transaction2.cdc took 362.811Β΅s
DidOpenTextDoc
[Info  - 18:43:46] parsing file:///Users/makoto/work/tmp/flow/week2/NFTv2.cdc took 540.36Β΅s
[Info  - 18:43:46] checking file:///Users/makoto/work/tmp/flow/week2/NFTv2.cdc took 5.069044ms
[Info  - 18:43:46] code lens called uri:file:///Users/makoto/work/tmp/flow/week2/Transaction2.cdc acct:f8d6e0586b0a20c7
[Info  - 18:43:46] code lens called uri:file:///Users/makoto/work/tmp/flow/week2/NFTv2.cdc acct:f8d6e0586b0a20c7
called execute command: cadence.server.createDefaultAccounts
create default acct [2]
[Info  - 18:43:54] submitting transaction [252 95 61 185 248 135 58 128 29 90 252 24 242 204 228 59 0 135 66 104 161 15 39 13 57 180 152 218 234 178 20 14]
[Info  - 18:43:54] Submitted transaction id=fc5f3db9f8873a801d5afc18f2cce43b00874268a10f270d39b498daeab2140e
[Info  - 18:43:54] submitting transaction [22 78 168 253 180 252 183 94 64 238 135 152 204 121 113 111 189 88 176 158 231 205 106 241 83 1 3 204 243 224 246 57]
[Info  - 18:43:54] Submitted transaction id=164ea8fdb4fcb75e40ee8798cc79716fbd58b09ee7cd6af1530103ccf3e0f639
called execute command: cadence.server.switchActiveAccount
set active acct [01cf0e2f2f715450]
DidChangeText changes: 1
[Info  - 18:43:58] parsing file:///Users/makoto/work/tmp/flow/week2/NFTv2.cdc took 572.156Β΅s
[Info  - 18:43:58] checking file:///Users/makoto/work/tmp/flow/week2/NFTv2.cdc took 4.442734ms
DidChangeText changes: 1
[Info  - 18:43:58] parsing file:///Users/makoto/work/tmp/flow/week2/Transaction2.cdc took 244.125Β΅s
[Info  - 18:43:58] checking file:///Users/makoto/work/tmp/flow/week2/Transaction2.cdc took 800.501Β΅s
[Info  - 18:43:58] code lens called uri:file:///Users/makoto/work/tmp/flow/week2/NFTv2.cdc acct:01cf0e2f2f715450
[Info  - 18:43:58] code lens called uri:file:///Users/makoto/work/tmp/flow/week2/Transaction2.cdc acct:01cf0e2f2f715450
called execute command: cadence.server.updateAccountCode
update acct code args: [file:///Users/makoto/work/tmp/flow/week2/NFTv2.cdc]
[Info  - 18:44:37] submitting transaction [170 174 254 53 1 185 31 254 246 96 56 240 183 12 62 251 107 212 241 252 132 94 242 141 244 212 203 71 145 66 95 11]
[Info  - 18:44:37] Submitted transaction id=aaaefe3501b91ffef66038f0b70c3efb6bd4f1fc845ef28df4d4cb4791425f0b
called execute command: cadence.server.submitTransaction
submit transaction args: [file:///Users/makoto/work/tmp/flow/week2/Transaction2.cdc]
[Info  - 18:45:14] submitting transaction [180 133 251 12 95 180 206 130 216 66 146 2 189 243 111 44 107 85 225 198 31 96 46 114 24 73 61 158 199 247 155 228]
[Error - 18:45:14] Request workspace/executeCommand failed.
  Message: client: rpc error: code = Unavailable desc = transport is closing
  Code: -32603 

I just pushed my change https://github.com/makoto/flow

Your array is empty. You can’t append like this:
ids[a] = Int(self.idCount)
If a value was at that index, your code would change it but it does not work for append.

Use .append instead like this:
ids.append(Int(self.idCount))

2 Likes

that was it. Thanks!

1 Like