Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cannot call function wrapped in a parexpr #22652

Open
zeozeozeo opened this issue Oct 25, 2024 · 2 comments
Open

cannot call function wrapped in a parexpr #22652

zeozeozeo opened this issue Oct 25, 2024 · 2 comments
Labels
Feature/Enhancement Request This issue is made to request a feature or an enhancement to an existing one.

Comments

@zeozeozeo
Copy link
Contributor

zeozeozeo commented Oct 25, 2024

V doctor:

V full version: V 0.4.8 5c65e58.a454f06
OS: windows, Microsoft Windows 10 Pro v19045 64-bit
Processor: 10 cpus, 64bit, little endian, 

getwd: C:\Users\user\Downloads
vexe: C:\Users\user\Desktop\Code\v\v.exe
vexe mtime: 2024-10-25 17:08:19

vroot: OK, value: C:\Users\user\Desktop\Code\v
VMODULES: OK, value: C:\Users\user\.vmodules
VTMP: OK, value: C:\Users\user\AppData\Local\Temp\v_0

Git version: git version 2.42.0.windows.1
Git vroot status: weekly.2024.41-127-ga454f064
.git/config present: true

CC version: Error: 'cc' is not recognized as an internal or external command,

operable program or batch file.


thirdparty/tcc status: thirdparty-windows-amd64 b425ac82

What did you do?
./v -g -o vdbg cmd/v && ./vdbg test.v

fn add(a int, b int) int {
	return a + b
}

fn main() {
	res := (add)(2, 4)
	println("2 + 4 = ${res}")
}

What did you expect to see?

2 + 4 = 6 printed to stdout or a better error message

What did you see instead?

test.v:6:16: error: unexpected token `,`, expecting `)`
    4 | 
    5 | fn main() {
    6 |     res := (add)(2, 4)
      |                   ^
    7 |     println("2 + 4 = ${res}")
    8 | }

Additional information

The following works, meaning that add can be used as an expression:

fn add(a int, b int) int {
    return a + b
}

fn main() {
    ptr := add
    res := ptr(2, 4)
    println("2 + 4 = ${res}")
}

As for other languages, this prints 2 + 4 = 6 in Go and in Rust:

package main

import "fmt"

func add(a, b int) int {
	return a + b
}

func main() {
	res := (add)(2, 4)
	fmt.Println("2 + 4 =", res)
}
fn add(a: i32, b: i32) -> i32 {
    return a + b;
}

fn main() {
    let res = (add)(2, 4);
    println!("2 + 4 = {res}");
}

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.

Huly®: V_0.6-21101

@JalonSolov
Copy link
Contributor

JalonSolov commented Oct 27, 2024

I am against this change. I see zero utility for something that can be confusing... is (add) supposed to mean to call add, or is it a cast, or is it something else?

Yes, I do know what it is meant to do, I simply don't know why the syntax should allow it... it just adds unnecessary complexity with no discernible benefit.

As far as other languages allowing it... V is not those other languages. It doesn't have to do what they do, with the same syntax.

The conclusion above, "The following works, meaning that add can be used as an expression:" is also incorrect. add is not being used as an expression in that example, the code is simply creating a function pointer and calling add through that pointer. That has nothing to do with an expression. It may appear as though it is being used as an expression, if you assume the syntax of other languages.

ptr := add in the example case is virtually the same as doing ptr := fn(a int, b int) int { return a + b } - an anonymous function pointer.

ptr := fn(a int, b int) int { return a + b }
res := ptr(2, 4)
println('2 + 4 = ${res}')

output is, as expected,

2 + 4 = 6

@Delta456
Copy link
Member

I don't believe we need a better error message for this

@felipensp felipensp added the Feature/Enhancement Request This issue is made to request a feature or an enhancement to an existing one. label Nov 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature/Enhancement Request This issue is made to request a feature or an enhancement to an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants