V: Single syntax for import

Created on 1 Jan 2020  路  8Comments  路  Source: vlang/v

Double syntax doesn't sound like V.

Is there a reason for 2 syntaxes for import?

Feature Request Discussion Question

Most helpful comment

Tooling would get simpler, if the only supported syntax was a single line import module.

Normally v files do not import many things, so I think import ( list_of_modules ) does not save many keystrokes.

Compare:

import (
    net.http
    os
    json
    filepath
)

to

import net.http
import os
import json
import filepath

In the second variant, you can find all the imports that a file uses just by grepping for ^import .

All 8 comments

Indeed it goes against V's philosophy of having it in one-way but it's also unnecessary to type too many import keywords when importing multiple modules and at the same time wasting whitespaces and parentheses when you are just gonna import a single module.

In short, there are specific cases wherein you would use the former and the latter.

@nedpals
I see your point.

I think that we could have a golden rule:
only one import statement per file (or none at all).

So for multiple imports the only syntax would be:

import (
module_a
module_b
)

For a single import there are 3 possibilities:


style A:

  • 100% uniform syntax

import ( // the only valid syntax for single import
my_module
)


style B:

  • almost 100% uniform syntax because everybody will write 'import' the same way

import my_module // the only valid syntax for single import


style C:

  • not uniform but much more uniform than the current syntax

import ( // valid syntax for single import
my_module
)

import my_module // also valid syntax for single import


At least this kind of code would be rejected by the compiler if using the only-one-import rule:

import module_a
import (
module_b
module_c
)
import module_d
import module_e
import (
module_f
)
import (
module_g
module_h
)

Yes, this has been discussed before, and I had the same thought.

It will be handled by vfmt. import foo if there's only one import, import (...) otherwise.

Tooling would get simpler, if the only supported syntax was a single line import module.

Normally v files do not import many things, so I think import ( list_of_modules ) does not save many keystrokes.

Compare:

import (
    net.http
    os
    json
    filepath
)

to

import net.http
import os
import json
import filepath

In the second variant, you can find all the imports that a file uses just by grepping for ^import .

I agree with you @spytheman

Just today I had to find where a certain module was imported, but it was impossible to do with grep.

I think

grep -E '^(\s+module.name|import module.name)$'
# -or-
grep -E '^(\s+|import )module.name$'

should do it, no?

@runeimp yes, your grep pattern will work for most cases. It however may have false positives, if your modules have enums in them, because enums also use the same syntax of list of identifiers, each on a separate line.

Closed. import () was deprecated in favor of grep-able import syntax (aka import modulea).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lobotony picture lobotony  路  3Comments

clpo13 picture clpo13  路  3Comments

radare picture radare  路  3Comments

PavelVozenilek picture PavelVozenilek  路  3Comments

penguindark picture penguindark  路  3Comments