V: Putting code outside main(), bypass main()

Created on 16 Sep 2020  路  3Comments  路  Source: vlang/v

V version: commit 5da698b (post 0.1.29)
OS:

What did you do?

In a single file:

fn main() {
    println("InMain: hello world!")
}

println("OutOfMain: hello world!")

What did you expect to see?

Compilation failure or

InMain: hello world!

What did you see instead?

OutOfMain: hello world!

Bug

Most helpful comment

Hmm... good catch. I don't recall anyone actually trying that before.

The reason it works now is that you don't have to have fn main() _at all_... if your entire program is

println('NoMain: Hello world!')

it works fine and prints the expected output.

However, if there _is_ a fn main(), V really should treat it as an error if there is code outside of any function.

All 3 comments

Hmm... good catch. I don't recall anyone actually trying that before.

The reason it works now is that you don't have to have fn main() _at all_... if your entire program is

println('NoMain: Hello world!')

it works fine and prints the expected output.

However, if there _is_ a fn main(), V really should treat it as an error if there is code outside of any function.

I did it by mistake with copy-paste.

Accidental discoveries are sometimes the most important. :-)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

radare picture radare  路  3Comments

arg2das picture arg2das  路  3Comments

taojy123 picture taojy123  路  3Comments

shouji-kazuo picture shouji-kazuo  路  3Comments

clpo13 picture clpo13  路  3Comments