# How to "copy-past" quote expression in a function?

**URL:** <https://discourse.julialang.org/t/how-to-copy-past-quote-expression-in-a-function/91480>\
**Category:** New to Julia\
**Tags:** metaprogramming, cuda\
**Created:** [December 9, 2022, 2:41pm UTC](https://discourse.julialang.org/t/how-to-copy-past-quote-expression-in-a-function/91480 "2022-12-09T14:41:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ludovic\_Dumoulin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ludovic_dumoulin/32/22414_2.png) [@Ludovic\_Dumoulin](https://discourse.julialang.org/u/Ludovic_Dumoulin)\
**Post date:** [December 9, 2022, 2:41pm UTC](https://discourse.julialang.org/t/how-to-copy-past-quote-expression-in-a-function/91480/1 "2022-12-09T14:41:14Z")

</div>

Hello,

I have some code to copy past in different function / kernel.  
I saw something about metaprogramming but I have to admit that this is very opaque for me …  
I would like to know if I can define a quote that contain the code and eval this quote in a function / cuda kernel ?

Background:  
I have 3 expressions

```julia
free_energy_1 = :(0.25*ar*ρ + ...)
free_energy_2 = :( - 0.5*ap*(ρ-ρcr)*P + ... )
free_energy_3 = :( - 0.5*ap*(ρ-ρcr)*P2 + ...) )

```

and I would like to define a variable in a function (CUDA kernel in this case) such that

```julia
f = eval(free_energy_1) + eval(free_energy_2) + eval(free_energy_3)

```

The Idea is to have many kernels where `f` can use only `free_energy_1`, or `free_ernegy_2`, or both, or… But I don’t want to repeat my code `0.25*ar*ρ + ...` in each of this kernel. Also repeating this code makes it difficult when I want to edit one of this `free_energy`, because then I have to change it everywhere…  
This is not working, that why I ask for help.

I can make some part working using macro, for example for indexing my 2D grid:

```julia
macro indexing_2D() 
    esc(quote
        i = (blockIdx().x - 1) * blockDim().x + threadIdx().x
        j = (blockIdx().y - 1) * blockDim().y + threadIdx().y
    end)
end

```

and then, inside my kernel I can call `@indexing_2D` and it declares my `i, j`  
This is not very useful, but I am trying to learn with “stupid” example.

Thank you for your help,  
At this point anything can be useful 🙂

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 9, 2022, 6:43pm UTC](https://discourse.julialang.org/t/how-to-copy-past-quote-expression-in-a-function/91480/2 "2022-12-09T18:43:21Z")

</div>

Don’t use metaprogramming for this. Just pass functions.

---

<div class="post-metadata">

**Author:** ![Ludovic\_Dumoulin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ludovic_dumoulin/32/22414_2.png) [@Ludovic\_Dumoulin](https://discourse.julialang.org/u/Ludovic_Dumoulin)\
**Post date:** [December 12, 2022, 9:11am UTC](https://discourse.julialang.org/t/how-to-copy-past-quote-expression-in-a-function/91480/3 "2022-12-12T09:11:08Z")

</div>

Thank you,  
I was thinking to do that, but I would make about 20 functions to pass in a kernel called 1024x1024x1’000’000 times.

I don’t know how it works in Julia but I am afraid about the time needed to call a fonction.  
Does `@inline` can help ?

I am sorry I know almost nothing about these things…

---

<div class="post-metadata">

**Author:** ![Ludovic\_Dumoulin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ludovic_dumoulin/32/22414_2.png) [@Ludovic\_Dumoulin](https://discourse.julialang.org/u/Ludovic_Dumoulin)\
**Post date:** [December 12, 2022, 10:17am UTC](https://discourse.julialang.org/t/how-to-copy-past-quote-expression-in-a-function/91480/4 "2022-12-12T10:17:32Z")

</div>

Adding 20 functions as arguments makes it very difficult to read…  
Is there a way of doing the same thing using metaprogramming ?  
I mean just quote and eval() ?  
The idea would be to have some lines of code to copy-past at the right place during compilation.

---

<div class="post-metadata">

**Author:** ![Ludovic\_Dumoulin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ludovic_dumoulin/32/22414_2.png) [@Ludovic\_Dumoulin](https://discourse.julialang.org/u/Ludovic_Dumoulin)\
**Post date:** [December 14, 2022, 5:20pm UTC](https://discourse.julialang.org/t/how-to-copy-past-quote-expression-in-a-function/91480/5 "2022-12-14T17:20:30Z")

</div>

I tried with functions, but know it takes 8 to 10 times more times to run…  
I am going back to my old code.

It seems there is no solution

Thank you for your help.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 14, 2022, 6:51pm UTC](https://discourse.julialang.org/t/how-to-copy-past-quote-expression-in-a-function/91480/6 "2022-12-14T18:51:11Z")

</div>

> [@Ludovic\_Dumoulin](#):
>
> I tried with functions, but know it takes 8 to 10 times more times to run…

Then you are probably doing something wrong; it’s possible to have highly optimized inlined code with higher order functions in Julia, as has been demonstrated in many practical applications (from Julia itself, which uses this technique heavily, to packages like DifferentialEquations or Gridap).

If you can show a **small self-contained** example of code that is running slower than you think it should, people here may be able to help you.
