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

Sum types of Fn and Optional Fn Types makes unknown function #22287

Closed
shininglove opened this issue Sep 23, 2024 · 5 comments
Closed

Sum types of Fn and Optional Fn Types makes unknown function #22287

shininglove opened this issue Sep 23, 2024 · 5 comments

Comments

@shininglove
Copy link

shininglove commented Sep 23, 2024

V doctor:

V full version: V 0.4.7 63957f2.b79aa02
OS: linux, Linux version 5.15.153.1-microsoft-standard-WSL2 (root@941d701f84f1) (gcc (GCC) 11.2.0, GNU ld (GNU Binutils) 2.37) #1 SMP Fri Mar 29 23:14:13 UTC 2024 (WSL 2)
Processor: 16 cpus, 64bit, little endian, Intel(R) Core(TM) i7-10700F CPU @ 2.90GHz

getwd: /home/shininglove/langs/v/bug_report
vexe: /home/shininglove/programs/v/v
vexe mtime: 2024-09-23 02:09:08

vroot: OK, value: /home/shininglove/programs/v
VMODULES: OK, value: /home/shininglove/.vmodules
VTMP: OK, value: /tmp/v_1000

Git version: git version 2.46.1
Git vroot status: weekly.2024.37-51-gb79aa020
.git/config present: true

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

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

module main

type DataFn = fn (name string) string

type DataFnOptional = fn (name string) ?string

type Data = DataFn | DataFnOptional

fn which_lang(name string) string {
	return name
}

fn which_lang_maybe(name string) ?string {
	return name
}

fn find_func(name string) ?Data {
	return match name {
		'vlang' { which_lang }
		'blang' { which_lang_maybe }
		else { none }
	}
}

fn main() {
	func := find_func('vlang')?
	option := func('options')
	println(option)
}

What did you expect to see?

a print of 'options'

What did you see instead?

src/main.v:27:2: warning: unused variable: `option`
   25 | fn main() {
   26 |     func := find_func('vlang')?
   27 |     option := func('options')
      |     ~~~~~~
   28 |     println(func)
   29 | }
src/main.v:27:12: error: unknown function: func
   25 | fn main() {
   26 |     func := find_func('vlang')?
   27 |     option := func('options')
      |               ~~~~~~~~~~~~~~~
   28 |     println(func)
   29 | }
src/main.v:27:9: error: assignment mismatch: 1 variable but `func()` returns 0 values
   25 | fn main() {
   26 |     func := find_func('vlang')?
   27 |     option := func('options')
      |            ~~
   28 |     println(func)
   29 | }
If the code of your project is in multiple files, try with `v src` instead of `v src/main.v`

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.

@yuyi98
Copy link
Member

yuyi98 commented Sep 23, 2024

The usage is wrong.

type DataFn = fn (name string) string

type DataFnOptional = fn (name string) ?string

type Data = DataFn | DataFnOptional

fn which_lang(name string) string {
	return name
}

fn which_lang_maybe(name string) ?string {
	return name
}

fn find_func(name string) ?Data {
	return match name {
		'vlang' { which_lang }
		'blang' { which_lang_maybe }
		else { none }
	}
}

fn main() {
	func := find_func('vlang')?
	if func is DataFn {
		option := func('options')
		println(option)
	} else if func is DataFnOptional {
		option := func('options')?
		println(option)
	}
	println(func)
}

PS D:\Test\v\tt1> v run .
options
Data(fn (string) string)

@shininglove
Copy link
Author

oh! thanks! I'll use this

Do you think there should be some kind of warning from the compiler to use this way?

@shininglove
Copy link
Author

shininglove commented Sep 23, 2024

The usage is wrong.

type DataFn = fn (name string) string

type DataFnOptional = fn (name string) ?string

type Data = DataFn | DataFnOptional

fn which_lang(name string) string {
	return name
}

fn which_lang_maybe(name string) ?string {
	return name
}

fn find_func(name string) ?Data {
	return match name {
		'vlang' { which_lang }
		'blang' { which_lang_maybe }
		else { none }
	}
}

fn main() {
	func := find_func('vlang')?
	if func is DataFn {
		option := func('options')
		println(option)
	} else if func is DataFnOptional {
		option := func('options')?
		println(option)
	}
	println(func)
}

PS D:\Test\v\tt1> v run .
options
Data(fn (string) string)

It seems like using this is only okay if you're not going to use the variable ('option' in this case) outside the if statements, but this breaks if you need to use it outside of it (which is what I need to do)

@felipensp
Copy link
Member

Not a bug. Thanks.

@shininglove
Copy link
Author

Thanks I was looking for this:

fn main() {
	func := find_func('vlang')?
	option := match func {
		DataFn { func() }
                DataFnOptional{ func()? }
	} 
	println(option)
}

in case someone has this same problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants