# Interpolate expression into macro

**URL:** <https://discourse.julialang.org/t/interpolate-expression-into-macro/24204>\
**Category:** General Usage\
**Tags:** metaprogramming\
**Created:** [May 14, 2019, 11:26am UTC](https://discourse.julialang.org/t/interpolate-expression-into-macro/24204 "2019-05-14T11:26:29Z")\
**Posts on this page:** 8\
**Page:** 2

<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:** [May 15, 2019, 12:07pm UTC](https://discourse.julialang.org/t/interpolate-expression-into-macro/24204/21 "2019-05-15T12:07:14Z")

</div>

Why not define the broadcast machinery for your `Particles` type and _force_ the user to use broadcasting in this kind of scenario?

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [May 15, 2019, 12:08pm UTC](https://discourse.julialang.org/t/interpolate-expression-into-macro/24204/22 "2019-05-15T12:08:57Z")

</div>

The problem is third-party code. I can’t make the user reimplement a third-party library to make their functions and objects work with my exotic number type 😕 (That, if possible, would have solved all my problems 🙂 )

---

<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:** [May 15, 2019, 12:12pm UTC](https://discourse.julialang.org/t/interpolate-expression-into-macro/24204/23 "2019-05-15T12:12:49Z")

</div>

But I wonder why you expect this code `p > 0 ? 1 : 0` to work for `Particles`. I can argue that it doesn’t work because it doesn’t make sense when some particles are positive and others are negative. Perhaps, it is more semantically sound then to have the user use a different Julia construct, e.g. `map`.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [May 15, 2019, 12:19pm UTC](https://discourse.julialang.org/t/interpolate-expression-into-macro/24204/24 "2019-05-15T12:19:54Z")

</div>

No I do not expect it to work and it doesn’t. But the result of the outer function is well-defined, it’s just a matter of getting there. `map` is a good option when particles appear on the input, I already have machinery for this [docs](https://github.com/baggepinnen/MonteCarloMeasurements.jl#monte-carlo-simulation-by-mappmap).

My example function implements (almost) `0.5(sign(x)+1)`. If `sign(p))` is called with particles, this returns the desired result (50/50 1/-1) It just so happen that `sign` is a function where the particles appear as arguments and defining this is easy. Arbitrary third-party functions, acting on objects with uncertain fields, that internally use branching, these are the problems. They are only well-defined if seen as a larger unit, as the branching inside are impossibl to handle unless julia implements support for fuzzy logic.

---

<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:** [May 15, 2019, 12:26pm UTC](https://discourse.julialang.org/t/interpolate-expression-into-macro/24204/25 "2019-05-15T12:26:07Z")

</div>

I think I am beginning to understand the problem. If branching is one (or the main) issue, how about a macro that transforms:

```julia
@fuzzy(p, f(p) ? 1 : 0)

```

to

```julia
if p isa Particles
    map(p -> f(p) ? 1 : 0, p)
else
    f(p) ? 1 : 0
end

```

?

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [May 15, 2019, 12:28pm UTC](https://discourse.julialang.org/t/interpolate-expression-into-macro/24204/26 "2019-05-15T12:28:36Z")

</div>

Well, once again it’s third-party code with third-party structures that cause problems… ☹

---

<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:** [May 15, 2019, 12:38pm UTC](https://discourse.julialang.org/t/interpolate-expression-into-macro/24204/27 "2019-05-15T12:38:57Z")

</div>

Well, if you can’t even _tell_ the users these constructs are supported and these are not, I am not sure how to do what you are trying to do. I was trying to figure out the basic constructs that are causing issues. Branching is one. Transforming code blocks can only done using macros or by Cassette overdubbing which acts on the function-call level of modularity, i.e. not sure how if statements can be transformed. This means that if dispatch falls short, macros are your only go-to solution. Parsing the whole function body and modifying it is another option, but again you need a _recipe_ for this transformation which may be harder to figure out in the generic case than if you just restrict your users to some specialized constructs, e.g. the `@fuzzy` macro for branching.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [May 15, 2019, 12:43pm UTC](https://discourse.julialang.org/t/interpolate-expression-into-macro/24204/28 "2019-05-15T12:43:18Z")

</div>

I have actually solved the problem somewhat satisfactorily, detailed in [this answer](https://discourse.julialang.org/t/interpolate-expression-into-macro/24204/10) and documented the limitations and potential solutions [here](https://github.com/baggepinnen/MonteCarloMeasurements.jl/tree/deconstruct#limitations). The only remaining issue is the world-age problem

[Previous page](https://discourse.julialang.org/t/interpolate-expression-into-macro/24204.md?page=1)
