【发布时间】:2020-07-14 22:13:43
【问题描述】:
我正在尝试简化与 C 的某些绑定,但我不确定这是否可能,我正在尝试做的是传递一个数组并期望在一个函数中接收,以便可以通过指定的类型构造一个对象在参数中或通过 ccall 调用正确的转换函数并初始化一个结构对象。
之前的代码,绑定的都是Vector3(v...)和Color(c...),有没有办法避免这种自动处理?
drawline(startPos, endPos, color) = ccall((:DrawLine3D, "my_lib"), Cvoid, (Vector3,Vector3,Color), Vector3(startPos...), Vector3(endPos...), Color(color...))
drawpoint([10,10,10],[100,100,100],[155,155,155,255]) # call example
是否可以通过这样的方式减少代码?:
struct Vector3
x::Cfloat
y::Cfloat
z::Cfloat
Vector3((x,y,z))=new(x,y,z)
end
#first attempt
#trying to call the Vector3 constructor without calling explicitly
drawpoint(startpos::Vector3,endpos::Vector3,color::Color) = ccall((:DrawPoint3D, "my_lib"), Cvoid, (Vector3,Vector3,Color), startpos,endpos,color)
#second attempt (should be the simplest way to go)
#trying to receive arrays so ccall can convert from list or tuple to Struct object
drawpoint(startpos,endpos,color) = ccall((:DrawPoint3D, "my_lib"), Cvoid, (Vector3,Vector3,Color), startpos,endpos,color)
这样的事情在 Julia 中是否可行?
【问题讨论】:
标签: constructor type-conversion julia implicit-conversion