Hi guys,
I am having a incorrect eval result. Can you help me with this?
type EE
x::Int
end
type DD
x::Float64
y::EE
end
ex = quote
x = $(Expr(:new, Int64, 1))
x += $(Expr(:new, Int64, 2))
y = x - $(Expr(:new, Int64, 1))
$(Expr(:new, DD, :(x + y), :($(Expr(:new, EE, :y)))))
end
eval(ex)
:new is a way to create DataType like Int, Float64, user defined types and so on. I always liked using new while creating a user defined type rather than call.
What is wrong about it?
Thank you for your insight, but still no reason why it is wrong.
And the eval is irrelevant, you can use a macro to return the expression i wrote. It will give you the same wrong result.
Allocates a new struct-like object. First argument is the type. The new pseudo-function is lowered to this, and the type is always inserted by the compiler. This is very much an internal-only feature, and does no checking. Evaluating arbitrary new expressions can easily segfault.
Chances are you should not be using this. Would an expression equivalent to
DD(x + y, EE(y))
do what you want? You can construct those very easily. But knowing more about the context (ie what you are trying to do, already asked by @dpsanders) would allow people to help you better.
ex = quote
x = $(Expr(:new, Int64, 1))
x += $(Expr(:new, Int64, 2))
y = x - $(Expr(:new, Int64, 1))
$(Expr(:new, DD, :(Float64(x + y)), :($(Expr(:new, EE, :y)))))
end