Repository navigation
Type-casting issue when providing a generic function as argument. #21132
Description
Describe the bug
I am not 100% shure if this is bug, but I am not able to create a function with another function (from a std library) as argument. In my opinion this should work, but everything I tried didnt. Ths snipped is the most basic one. I tried with setting the generic to "f64" (the error message than refers surprisingly to the module like "math.f64"), or try to cast the function type (more errors of course).
I hope that I have made a mistake, but this type of functions would be very helpfull to avoid unnecessary (escecially for lazy people like me).
Reproduction Steps
Code: https://play.vlang.io/p/0a82f72d9f
// function-as-argument to reduce code
import math
type Vec5 = struct {
x f64
y f64
z f64
a f64
b f64
}
fn from(s f64) Vec5 {
return Vec5{s, s, s, s, s}
}
fn (v Vec5) abs_oldstyle() Vec5 {
return Vec5{math.abs(v.x), math.abs(v.y), math.abs(v.z), math.abs(v.a), math.abs(v.b)}
}
fn (v Vec5) generic_new(f fn (f64) f64) Vec5 {
return Vec5{f(v.x), f(v.y), f(v.z), f(v.a), f(v.b)}
}
fn main() {
println('### start')
mut v := from(-1)
println(v)
println(v.abs_oldstyle()) // ok
// error: cannot use `fn (T) T` as `fn (f64) f64` in argument 1 to `Vec5.generic`
println(v.generic_new(math.abs)) // error
println('### end')
}Expected Behavior
No error and the same result for "abs_oldstyle()" and "generic_new(math.abs)".
Current Behavior
Output:
code.v:32:23: error: cannot use `fn (T) T` as `fn (f64) f64` in argument 1 to `Vec5.generic_new`
30 | println(v.abs_oldstyle()) // ok
31 | // error: cannot use `fn (T) T` as `fn (f64) f64` in argument 1 to `Vec5.generic`
32 | println(v.generic_new(math.abs)) // error
| ~~~~~~~~
33 |
34 | println('### end')
Exited with error status 1
Possible Solution
No response
Additional Information/Context
No response
V version
V 0.4.5 872bcbc
Environment details (OS name and version, etc.)
V full version: V 0.4.5 872bcbc
OS: linux, Debian GNU/Linux 11 (bullseye) (VM)
Processor: 1 cpus, 64bit, little endian, Intel(R) Xeon(R) CPU E5-2686 v4 @ 2.30GHz
getwd: /home/admin/playground
vexe: /home/admin/v/v
vexe mtime: 2024-03-28 12:23:26
vroot: OK, value: /home/admin/v
VMODULES: OK, value: .vmodules
VTMP: OK, value: /tmp/v_0
Git version: git version 2.30.2
Git vroot status: Error: fatal: detected dubious ownership in repository at '/home/admin/v'
To add an exception for this directory, call:
git config --global --add safe.directory /home/admin/v
.git/config present: true
CC version: cc (Debian 10.2.1-6) 10.2.1 20210110
thirdparty/tcc status: Error: fatal: detected dubious ownership in repository at '/home/admin/v/thirdparty/tcc'
To add an exception for this directory, call:
git config --global --add safe.directory /home/admin/v/thirdparty/tcc
Error: fatal: detected dubious ownership in repository at '/home/admin/v/thirdparty/tcc'
To add an exception for this directory, call:
git config --global --add safe.directory /home/admin/v/thirdparty/tcc
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.
Activity
debilosaurus commentedon Mar 29, 2024
I have found a fix, but I am still thinking that some "autocast" feature dont work properly:
Change:
To:
debilosaurus commentedon Mar 30, 2024
Again: This is no priority bug, maybe it isn't an issue at all.
But here is another hint why I think that there must be casting issues; this time with an interface type.
Backround: For testing purposes with signed distance functions I wrote my own vector implementation. Then I tried to reduce the code to the absolute minimum - with interfaces . my goal is to generalize it in a simple way to more dimensions or other implementations (eg. array). But vlang sometimes is a beast ...
[-]Type-casting issue when providing a function as argument.[/-][+]Type-casting issue when providing a generic function as argument.[/+]debilosaurus commentedon Nov 10, 2024
After some time spending to other things, I tried to find the cause of this behavior. I hope this helps to improve your language.
type-casting issue when providing a generic function as argu... (fixes …
checker: fix type-casting issue when providing generic function as ar…
type-casting issue when providing a generic function as argu... (fixes …
checker: fix type-casting issue with generic function as argument (fixes
checker: type-casting issue when providing a generic function as argu…
checker: fix type-casting issue with generic function as argument (fixes
checker: fix type-casting issue when providing generic function as ar…
checker: fix type-casting issue when providing generic function as ar…