Open SheatNoisette opened 2 years ago
Hmm. The voidptr
is just a pointer type in V. Not sure you can/should cast a void pointer to a function in V?
It has no information about parameters or return requirements for a function. Define the variable specifying the type of function to accept.
func fn ()
Is something like this you are after?
module main
struct Struct_voidptr{
func fn () // func is a function with no parameters and no return
}
fn function() {
println("Function!")
}
fn main() {
fun := &function
sct := &Struct_voidptr{
func: fun // same as *fun
}
fun()
sct.func() // call the stored function from structure on heap
}
output
Function!
Function!
You don't actually need the address operator and can use the address to the function or access the address directly from the copy in the variable.
fun := function // fn ()
sct := &Struct_voidptr{
func: fun // or use 'function' directly
}
Sorry for being verbose. Old habits die hard.
Compilation should have thrown a compiler error and not the C error. This should never happen.
I was just messing around with the compiler and found this. I was hoping for a V error and instead it generated C code where it shouldn't.
I was just messing around with the compiler and found this. I was hoping for a V error and instead it generated C code where it shouldn't.
Thought that might have been the case. Happy to delete the post if needed.
No need to delete the post. It's a good example of a function pointer. Btw, I may be wrong but I don't think the doc explain this concept ?
Anyway, I think this is a bug.
V doctor:
What did you do?
v -g -o vdbg cmd/v && vdbg bug.v
What did you expect to see?
A generated executable.
What did you see instead? V generated invalid C code, no warning(s) or error(s) from V compiler.