使用闭包数据序列化lambda函数时出错

时间:2015-10-25 20:21:42

标签: julia

我使用这样的代码:

p = _belineInterpolateGrid( map( p -> sin(norm(p)), grid ), grid )
f = open("/data/test.function", "w")
serialize( f, p )
close(f)
p0 = deserialize( open("/data/test.function", "r") )

其中_belineInterpolateGrid是

function _belineInterpolateGrid(PP, Grid)
    ...
    P = Array(Function, N-1, M-1);
    ...
    poly = (x,y) -> begin
        i_x, i_y =  i(x, y);
        return P[i_x, i_y](x, y);
    end
return poly

现在,由于某些v0.4,a有错误:

ERROR: MethodError: `convert` has no method matching
convert(::Type{LambdaStaticData}, ::Array{Any,1})
This may have arisen from a call to the constructor LambdaStaticData(...),
since type constructors fall back to convert methods.
Closest candidates are:
call{T}(::Type{T}, ::Any)
convert{T}(::Type{T}, ::T)
 ...
 in deserialize at serialize.jl:435

为什么会这样?它是错误以及如何解决它?

2 个答案:

答案 0 :(得分:1)

这看起来像朱莉娅中的一个错误给我,看起来它已经修复了v0.4.6。尝试升级到该版本或更新版本,看看问题是否仍然存在。

答案 1 :(得分:0)

你回来了一个lambda,这就是原因。无法判断它是否是一个错误(你可以序列化一个lambda,但你不能反序列化它?)。

您可以通过将“在x,y处获取插值”定义为类型来避免

import Base: getindex
type MyPoly
    thepoly
end

function getindex(p::MyPoly, x::Int, y::Int)
    p.thepoly[x+5*y]
end
function getindex(p::MyPoly, I...)
    p.thepoly[I...]
end
function call(p::MyPoly, v)
    #collect helps keep eltype(ans) == Int
    powered_v = map( i->v^i, collect(
                     take(countfrom(),size(p.thepoly,1))))
    powered_v.*p.thepoly
end


p=MyPoly(collect(1:10))
println(p[1])
f = open("serializedpoly", "w")
serialize( f, p)
close(f)
p0 = deserialize( open("serializedpoly", "r"))
println(p[1,1])

v=call(p, 4) #evaluate poly on 4

编辑:为call

添加了扩展程序