# Macro that inlines functions

**URL:** https://discourse.julialang.org/t/macro-that-inlines-functions/105391
**Category:** General Usage
**Created:** [October 25, 2023, 6:13pm UTC](https://discourse.julialang.org/t/macro-that-inlines-functions/105391 "2023-10-25T18:13:24Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![f.ij](https://avatars.discourse-cdn.com/v4/letter/f/8491ac/32.png) [@f.ij](https://discourse.julialang.org/u/f.ij)
#### Post date: [October 25, 2023, 6:13pm UTC](https://discourse.julialang.org/t/macro-that-inlines-functions/105391/1 "2023-10-25T18:13:24Z")

</div>

I want to use some inlinable functions with @turbo for cleaner syntax, [which currently does not work](https://discourse.julialang.org/t/loop-vectorization-syntax-very-limiting/105387/5).

Is it possible to write a macro that gets an expression, and then for every function that should inline, replaces it by the function body? This way I could use my inlinable functions and write something like @inline\_expand @turbo expr.

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [October 25, 2023, 6:16pm UTC](https://discourse.julialang.org/t/macro-that-inlines-functions/105391/2 "2023-10-25T18:16:08Z")

</div>

not really. Macro can’t see runtime value thus it won’t know what’s the method body to “paste” in place of your function call

---

<div class="post-metadata">

### Author: ![f.ij](https://avatars.discourse-cdn.com/v4/letter/f/8491ac/32.png) [@f.ij](https://discourse.julialang.org/u/f.ij)
#### Post date: [October 25, 2023, 6:17pm UTC](https://discourse.julialang.org/t/macro-that-inlines-functions/105391/3 "2023-10-25T18:17:41Z")

</div>

I’m not sure I completely understand, the expression that should be inlined should be completely known at compile time, right?

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [October 25, 2023, 6:19pm UTC](https://discourse.julialang.org/t/macro-that-inlines-functions/105391/4 "2023-10-25T18:19:50Z")

</div>

macros run on source code before anything happens, not during compile time

---

<div class="post-metadata">

### Author: ![f.ij](https://avatars.discourse-cdn.com/v4/letter/f/8491ac/32.png) [@f.ij](https://discourse.julialang.org/u/f.ij)
#### Post date: [October 25, 2023, 6:21pm UTC](https://discourse.julialang.org/t/macro-that-inlines-functions/105391/5 "2023-10-25T18:21:58Z")

</div>

My bad, you’re right. Then it would not work if there’s any dispatch going on. However, in my case I just want to write some functions, basically as an alias to make the code a bit cleaner and semantically easier to understand. The macro could scan the source code to find the function body in this case.

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [October 25, 2023, 6:22pm UTC](https://discourse.julialang.org/t/macro-that-inlines-functions/105391/6 "2023-10-25T18:22:40Z")

</div>

you can use [GitHub - rafaqz/Mixers.jl: Julia mixin macros. Mixed, not stirred](https://github.com/rafaqz/Mixers.jl) to “puke” the source code body into the location you want. `@pour`

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [October 25, 2023, 6:25pm UTC](https://discourse.julialang.org/t/macro-that-inlines-functions/105391/7 "2023-10-25T18:25:08Z")

</div>

Not sure I understand your question correctly, but the `@inline` macro is supposed to work at the call site as well: [Essentials · The Julia Language](https://docs.julialang.org/en/v1/base/base/#Base.@inline)

> ```julia
> @inline block
> 
> ```
> 
> Give a hint to the compiler that calls within `block` are worth inlining.
> 
> ```julia
> # The compiler will try to inline `f`
> @inline f(...)
> 
> # The compiler will try to inline `f`, `g` and `+`
> @inline f(...) + g(...)
> 
> ```

---

<div class="post-metadata">

### Author: ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)
#### Post date: [October 25, 2023, 6:41pm UTC](https://discourse.julialang.org/t/macro-that-inlines-functions/105391/8 "2023-10-25T18:41:03Z")

</div>

Note for your hypothetical macro to fulfil its purpose it would need to be expanded _before_ the outer `@turbo`. I think (but do not know) that the outermost macro is expanded first. If that is the case then even your hypothetical macro solution could not work.

---

<div class="post-metadata">

### Author: ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)
#### Post date: [October 25, 2023, 6:44pm UTC](https://discourse.julialang.org/t/macro-that-inlines-functions/105391/9 "2023-10-25T18:44:34Z")

</div>

IIUC, the issue that @f.ij wants to do something like:

```julia
@turbo ....
    myfunction(...)
end

```

to write cleaner code inside the loop. However in that case LoopVectorization.jl cannot see inside the function call to optimize stuff.

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [October 25, 2023, 6:49pm UTC](https://discourse.julialang.org/t/macro-that-inlines-functions/105391/10 "2023-10-25T18:49:09Z")

</div>

oh, in that case `@pour` won’t work, because macro evaluation is outside-in (opposite of function evaluations)

---

<div class="post-metadata">

### Author: ![f.ij](https://avatars.discourse-cdn.com/v4/letter/f/8491ac/32.png) [@f.ij](https://discourse.julialang.org/u/f.ij)
#### Post date: [October 26, 2023, 7:40am UTC](https://discourse.julialang.org/t/macro-that-inlines-functions/105391/11 "2023-10-26T07:40:42Z")

</div>

> [@jling](#):
>
> oh, in that case `@pour` won’t work, because macro evaluation is outside-in (opposite of function evaluations)

I assumed the same thing but it does seem to work, I’m not a great fan of the syntax though. Moreover I’m fine with writing a macro that wraps around @turbo.
