-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Comments
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) |
oh! thanks! I'll use this Do you think there should be some kind of warning from the compiler to use this way? |
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) |
Not a bug. Thanks. |
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 |
V doctor:
What did you do?
./v -g -o vdbg cmd/v && ./vdbg src/main.v
What did you expect to see?
a print of 'options'
What did you see instead?
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.
The text was updated successfully, but these errors were encountered: