Example in documentation causes stack overflow
Closed this issue · 5 comments
I'm not able to run the example in the documents. It produces a stack overflow.
julia> using StrPack
julia> @struct type TestStruct
int1::Int32
float1::Float32
end
isequal (generic function with 38 methods)
julia> TestStruct(i, f) = TestStruct(convert(Int32, i), convert(Float32, f))
TestStruct (constructor with 1 method)
julia> s = TestStruct(-1, 1.2)
ERROR: stack overflow
in TestStruct at none:1 (repeats 80000 times)
This is caused by JuliaLang/julia#4026. The default constructor with convert
calls are automatically created, so you don't need to manually write it anymore. When the default constructor now accepts any argument, your constructor overwrites it and calls itself recursively, and the result is a StackOverflow
.
Beautiful. The following works:
using StrPack
@struct type TestStruct
int1::Int32
float1::Float32
end
# TestStruct(i, f) = TestStruct(convert(Int32, i), convert(Float32, f))
s = TestStruct(-1, 1.2)
iostr = IOBuffer()
pack(iostr, s)
ccall((:getvalues,"libteststruct"), Void, (Ptr{Void},), iostr.data)
seek(iostr, 0)
s2 = unpack(iostr, TestStruct)
println(s2)
On Tue, May 6, 2014 at 11:08 AM, Ivar Nesje notifications@github.comwrote:
This is caused by JuliaLang/julia#4026JuliaLang/julia#4026.
The default constructor with convert calls are automatically created, so
you don't need to manually write it anymore. When the default constructor
now accepts any argument, your constructor overwrites it and calls itself
recursively, and the result is a StackOverflow.—
Reply to this email directly or view it on GitHubhttps://github.com//issues/12#issuecomment-42337814
.
Madeleine Udell
PhD Candidate in Computational and Mathematical Engineering
Stanford University
www.stanford.edu/~udell
Good to know I'm not the only one who had this exact same problem... JuliaLang/julia#6756
You are definitely not the only one, and I'm actually a little surprised that we have not seen more problems from this change (people have probably figured it out themselves). It's great that issues are raised in cases like this, especially when there is a simple documentation fix that makes it easier for the next person trying to figure out how things work.