vlang / v

Simple, fast, safe, compiled language for developing maintainable software. Compiles itself in <1s with zero library dependencies. Supports automatic C => V translation. https://vlang.io
MIT License
35.77k stars 2.16k forks source link

:= is allowed in const declaration #22575

Closed gechandesu closed 1 week ago

gechandesu commented 1 week ago

Describe the bug

V compiles the program with the := sign in the constant declaration. This is inconsistent and confusing. The only option described in the documentation is to use the equal sign.

Reproduction Steps

cnst.v:

module main

const myconst1 = 1
const myconst2 := 2

fn main() {
    println(myconst1)
    println(myconst2)
}
$ v cnst.v
$ ./cnst
1
2

Expected Behavior

Syntax error

Current Behavior

Constants declared with := works fine

Possible Solution

Disallow using := in const

Additional Information/Context

No response

V version

V 0.4.8 5ec9bb5

Environment details (OS name and version, etc.)

V full version: V 0.4.8 5ec9bb5 OS: linux, Linux version 6.6.8-arch1-1 (linux@archlinux) (gcc (GCC) 13.2.1 20230801, GNU ld (GNU Binutils) 2.41.0) #1 SMP PREEMPT_DYNAMIC Thu, 21 Dec 2023 19:01:01 +0000 Processor: 16 cpus, 64bit, little endian, 12th Gen Intel(R) Core(TM) i5-1240P

getwd: /home/ge vexe: /home/ge/.vlang/v vexe mtime: 2024-10-18 16:32:24

vroot: OK, value: /home/ge/.vlang VMODULES: OK, value: /home/ge/.vmodules VTMP: OK, value: /tmp/v_1000

Git version: git version 2.46.1 Git vroot status: weekly.2024.42-22-g5ec9bb54 .git/config present: true

CC version: cc (GCC) 14.2.1 20240910 thirdparty/tcc status: thirdparty-linux-amd64 0134e9b9

[!NOTE] You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote. Other reactions and those to comments will not be taken into account.

Delta456 commented 1 week ago

Good find. I will take a look

JalonSolov commented 1 week ago

Yes, it is allowed, but v fmt will remove the :, one of the many changes it can make while trying to make your code more compliant with latest V syntax.

Delta456 commented 1 week ago

Yes, it is allowed, but v fmt will remove the :, one of the many changes it can make while trying to make your code more compliant with latest V syntax.

A warning should be there at least

JalonSolov commented 1 week ago

V should probably warn you about it, but perhaps not... there has been discussion about making := the correct form for const values, to make them move consistent syntax with regular variables.

JalonSolov commented 1 week ago

@medvednikov @spytheman - what do you think? Should V add a warning for this?

spytheman commented 1 week ago

No, it is a deliberate feature, not a bug.

There should not be a warning, a notice etc at all.

v fmt -w will rewrite it to =, but while you are prototyping, it is very convenient to be able to extract local variables from functions to constants with minimal friction.