# Interesting Eval Problem

**URL:** <https://discourse.julialang.org/t/interesting-eval-problem/7428>\
**Category:** General Usage\
**Created:** [December 1, 2017, 3:16am UTC](https://discourse.julialang.org/t/interesting-eval-problem/7428 "2017-12-01T03:16:01Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)\
**Post date:** [December 1, 2017, 3:16am UTC](https://discourse.julialang.org/t/interesting-eval-problem/7428/1 "2017-12-01T03:16:01Z")

</div>

Hi guys,  
I am having a incorrect eval result. Can you help me with this?

```nohighlight

    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)

```

the result seems to be:

```nohighlight
DD(2.5e-323,EE(2))

```

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [December 1, 2017, 5:54am UTC](https://discourse.julialang.org/t/interesting-eval-problem/7428/2 "2017-12-01T05:54:35Z")

</div>

- Why are you using `new`?
- But more importantly, what exactly are you trying to do? `eval` is rarely the correct answer.

---

<div class="post-metadata">

**Author:** ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)\
**Post date:** [December 1, 2017, 6:24am UTC](https://discourse.julialang.org/t/interesting-eval-problem/7428/3 "2017-12-01T06:24:53Z")

</div>

```nohighlight
begin 
     x=1
     x+=2
     y=x-1
     DD(x+y,EE(y)) 
end

```

I manually translated the code above to Exprs with :new and I am trying to understand why it doesn’t give me the right answer.  
Thanks

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [December 1, 2017, 6:32am UTC](https://discourse.julialang.org/t/interesting-eval-problem/7428/4 "2017-12-01T06:32:21Z")

</div>

Why are you using `new` at all though? (I mean, where did that come from.)

---

<div class="post-metadata">

**Author:** ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)\
**Post date:** [December 1, 2017, 6:41am UTC](https://discourse.julialang.org/t/interesting-eval-problem/7428/5 "2017-12-01T06:41:03Z")

</div>

: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?

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [December 1, 2017, 6:49am UTC](https://discourse.julialang.org/t/interesting-eval-problem/7428/6 "2017-12-01T06:49:36Z")

</div>

I have never seen `new` used like that, except in the one particular case of so-called “inner constructors” for user-defined types.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [December 1, 2017, 6:49am UTC](https://discourse.julialang.org/t/interesting-eval-problem/7428/7 "2017-12-01T06:49:54Z")

</div>

It is unnecessary and redundant.

---

<div class="post-metadata">

**Author:** ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)\
**Post date:** [December 1, 2017, 6:53am UTC](https://discourse.julialang.org/t/interesting-eval-problem/7428/8 "2017-12-01T06:53:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 1, 2017, 7:15am UTC](https://discourse.julialang.org/t/interesting-eval-problem/7428/9 "2017-12-01T07:15:37Z")

</div>

See the [manual](https://docs.julialang.org/en/latest/devdocs/ast/) on `:new`:

> 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

```julia
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.

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [December 1, 2017, 8:38am UTC](https://discourse.julialang.org/t/interesting-eval-problem/7428/10 "2017-12-01T08:38:34Z")

</div>

FWIW, if you do this

```julia
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

```

it works as expected.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [December 1, 2017, 9:57am UTC](https://discourse.julialang.org/t/interesting-eval-problem/7428/11 "2017-12-01T09:57:20Z")

</div>

```julia
julia> reinterpret(Float64, 5)
2.5e-323

```

---

<div class="post-metadata">

**Author:** ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)\
**Post date:** [December 1, 2017, 2:49pm UTC](https://discourse.julialang.org/t/interesting-eval-problem/7428/12 "2017-12-01T14:49:03Z")

</div>

Thank you all!  
You have been greatly helpful.
