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

lambda doesn't work as struct field #19428

Closed
islonely opened this issue Sep 23, 2023 · 2 comments · Fixed by #19436
Closed

lambda doesn't work as struct field #19428

islonely opened this issue Sep 23, 2023 · 2 comments · Fixed by #19436
Labels
Bug This tag is applied to issues which reports bugs.

Comments

@islonely
Copy link
Contributor

islonely commented Sep 23, 2023

Describe the bug

Can't use lambda as struct field.

Reproduction Steps

module main

import cli
import os

const (
	no_args = '
Usage: cssml [options] <file>

Options:
	-h, --help     output usage information
	-v, --version  output the version number'
)

fn main() {
	mut app := cli.Command{
		name: 'cssml',
		description: 'A CSSML compiler - CSS and HTML in one file.',
		execute: |cmd| println(no_args)
	}
	app.setup()
	app.parse(os.args)
}

Expected Behavior

Expected code to compile and print the no_args const when executed without cli arguments.

Current Behavior

error: invalid expression: unexpected token `|`

Possible Solution

No response

Additional Information/Context

I have a fix for this, but it will allow lambdas anywhere an anonymous function is expected.

V version

V 0.4.1 a685088

Environment details (OS name and version, etc.)

v full version: V 0.4.1 a685088
Os: linux, Ubuntu 20.04.6 LTS (VM)
Processor•
8 cpus, 64bit, little endian, AMD EPYC 7763 64-Core Processor
getwd: /workspaces/assmi vexe: /opt/v/v
vexe mime: 2023-09-23 23:29:02
vroot: OK, value: /opt/v
MODULES: OK, value: /home/codespace/.vmodules
VTMP: OK, value: /tmp/v 1000
Git version: git version 2.41.0
Git vroot status: weeklv.2023.33-210-aa685088€ .git/config present: true
CC version: cC (Ubuntu 9.4.0-lubuntul~20.04.1) 9.4.0
thirdparty/tcc status: thirdparty-linux-amd64 12£392c3

Important

You can vote for this issue using the 👍 reaction. More votes increase the issue's priority
for developers.

Take into account that only the 👍 reaction counts as a vote.
Only reactions to the issue itself will be counted as votes, not comments.

@islonely islonely added the Bug This tag is applied to issues which reports bugs. label Sep 23, 2023
@islonely
Copy link
Contributor Author

After looking into the compiler to try and fix myself: This is caused by the parser (and checker once parser check is removed) not allowing lambdas as values outside of function args. Kinda makes sense I suppose, but on the other hand it doesn't make sense that they aren't allowed anywhere an anonymous function type is already declared. In that case, this is technically not a bug (compiler's doing what it's supposed to do).

@islonely
Copy link
Contributor Author

islonely commented Sep 24, 2023

This code already exists to block lambdas from just be assigned to values where they shouldn't belong. var := |x,y| x+y would not be allowed, but var = |x,y| x+y would be if mut var := fn(x int, y int) int { return x+y } was declared earlier. Also fixes, the issue mentioned in the original post of struct field values not working.

if exp_sym.kind != .function {
	c.error('a lambda expression was used, but `${exp_sym.kind}` was expected', node.pos)
	return ast.void_type
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant