crystal-book
crystal-book copied to clipboard
Documentation on `fun` argument conversion is incorrect
Note that there are no implicit conversions (except
to_unsafe
, which is explained later) when invoking a C function: you must pass the exact type that is expected. For integers and floats you can use the variousto_...
methods.
But this is not true:
lib LibC
alias Int = Int32
fun exit(status : Int) : NoReturn
end
LibC.exit(69_u8) # okay
LibC.exit(69_i128) # okay
More surprising is that it is okay to pass a BigInt
here too, but not its unsafe counterpart:
require "big"
LibC.exit(69.to_big_i) # okay
LibC.exit(69.to_big_i.to_unsafe) # Error: argument 'x0' of 'LibC#exit' must be Int32, not Pointer(LibGMP::MPZ)
So something else must be going on.