Language error: Forc compiling issues

Topic discussion checklist

  • Hi guys. I seem to have issues with compiling when I use latest force version.

  • Is there anyway I can solve this. The error messages are provided below.

  • error
    → /home/ahmed/fuel-project/counter-contract/src/main.sw:18:9
    |
    16 |
    17 | fn count() → u64 {
    18 | storage.counter
    | ^^^^^^^^^^^^^^^ Mismatched types.
    expected: u64
    found: StorageKey.
    help: Implicit return must match up with block’s type.
    19 | }
    |


error
→ /home/ahmed/fuel-project/counter-contract/src/main.sw:18:9
|
16 |
17 | fn count() → u64 {
18 | storage.counter
| ^^^^^^^^^^^^^^^ Mismatched types.
expected: u64
found: StorageKey.
help: Function body’s return type does not match up with its return type annotation.
19 | }
|


error
→ /home/ahmed/fuel-project/counter-contract/src/main.sw:23:9
|
21 |
22 | fn increment() {
23 | storage.counter = storage.counter + 1;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This expression is not valid on the left hand side of a reassignment.
24 | }
25 | }
|


Aborting due to 3 errors.
error: Failed to compile counter-contract

  • Any other relevant information

Hey @ayobamy you cannot directly mutate or access storage variables like that. You have to use the .write() or .read() functions.

For example for your first two errors

#[storage(read)]
fn count() -> u64 {
    storage.counter.read()
}

for your last error

#[storage(read, write)]
fn increment() {
    let incremented = storage.counter.read() + 1;
    storage.counter.write(incremented);
}

Take another look at this section in the quickstart :slight_smile:

1 Like

Alright, I will do that.

Thanks a lot for the prompt response.

1 Like

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.