V: unused variables should not be allowed

Created on 20 Apr 2019  路  5Comments  路  Source: vlang/v

Put follow code into this page https://vlang.io/play

fn main() {
    types := ['game', 'web', 'tools', 'GUI']
    mut types2 := types[0]
    for typ in types {
        println('Hello, $typ developers!')
    }
}

The program run successfully.

I think the compiler should say that unused variable types2
If a variable is only defined, but not read, the compiler should report an error to prevent the programer write the wrong variable name. (like golang)

Most helpful comment

I think this should be a warning, not an error - compilation should continue. IMO giving an error discourages the programmer from compiling code before it's ready to test. Compiling regularly whilst developing code is a good way to catch errors early. As an error the programmer is forced to comment out code or add a println for any unused variables, which is awkward.

All 5 comments

Agreed. This is a big change, will do this later.

Fixed:

`types2` declared and not used 

I think this should be a warning, not an error - compilation should continue. IMO giving an error discourages the programmer from compiling code before it's ready to test. Compiling regularly whilst developing code is a good way to catch errors early. As an error the programmer is forced to comment out code or add a println for any unused variables, which is awkward.

The compiler error declared and not used had found a lot of bugs in my golang code.
Some of bugs looks like the code is only write first part , and forget to write the remain part. Forget to write some part of the code is always a bug...
The compiler can find more abnormal things, the more bugs may be found because of abnormal things.
It is always good to find bug earlier and by compiler.
If you do not want the code, just comment them out to tell the compiler that you do not need it.

The compiler error declared and not used had found a lot of bugs in my golang code

A warning (on by default) would find them too.

If you do not want the code,

I want to see if an expression assigned to an unused variable compiles.

just comment them out to tell the compiler that you do not need it

Beside my use case above, commenting out that line then likely means another variable becomes unused. It is awkward.

Was this page helpful?
0 / 5 - 0 ratings