# Help with @generated

**URL:** <https://discourse.julialang.org/t/help-with-generated/51829>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 14, 2020, 10:23pm UTC](https://discourse.julialang.org/t/help-with-generated/51829 "2020-12-14T22:23:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![gmouts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gmouts/32/9896_2.png) [@gmouts](https://discourse.julialang.org/u/gmouts)\
**Post date:** [December 14, 2020, 10:23pm UTC](https://discourse.julialang.org/t/help-with-generated/51829/1 "2020-12-14T22:23:49Z")

</div>

Apart from what I am asking below, I would really appreciate if someone could point me to some resource that explains the usage of the macro `generated`.

I want to write a function that will loop through an array and will do something. I try to optimise it, so I looked at the evalpoly function from Base. The code is the following:

```julia
function evalpoly(x, p::Tuple)
    if @generated
        N = length(p.parameters::Core.SimpleVector)
        ex = :(p[end])
        for i in N-1:-1:1
            ex = :(muladd(x, $ex, p[$i]))
        end
        ex
    else
        _evalpoly(x, p)
    end
end

```

I have hard time reading this. First of all I would expect that evalpoly is defined as `@generated function`, however the macro `@generated` appears in a conditional. What does this mean exactly?

Then it’s the line `N = length(p.parameters::Core.SimpleVector)`. I undertstand what it evaluates to, but I don’t understand why. What is `p.parameters`?

Then after that what does the line `ex = :(muladd(x, $ex, p[$i]))` use a quote?

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [December 14, 2020, 11:32pm UTC](https://discourse.julialang.org/t/help-with-generated/51829/2 "2020-12-14T23:32:16Z")

</div>

> [@gmouts](#):
>
> I have hard time reading this. First of all I would expect that evalpoly is defined as `@generated function` , however the macro `@generated` appears in a conditional. What does this mean exactly?

> [@gmouts](#):
>
> Then after that what does the line `ex = :(muladd(x, $ex, p[$i]))` use a quote?

This is an optionally generated function. The manual explains these quite well: [Metaprogramming · The Julia Language](https://docs.julialang.org/en/v1/manual/metaprogramming/#Optionally-generated-functions)

> [@gmouts](#):
>
> Then it’s the line `N = length(p.parameters::Core.SimpleVector)` . I undertstand what it evaluates to, but I don’t understand why. What is `p.parameters` ?

The `parameters` field contains the type parameters of a `DataType`, which is stored as a `Core.svec` (a barebones array type, mainly used by parts of the compiler)., i.e. for `Tuple{Int,Float64}` it would be `Core.svec(Int, Float64)`. This is only an implementation detail of how types are represented in the compiler though, so it should be used with care.
