# Call macro in condition on run time

**URL:** <https://discourse.julialang.org/t/call-macro-in-condition-on-run-time/90434>\
**Category:** New to Julia\
**Created:** [November 18, 2022, 9:59am UTC](https://discourse.julialang.org/t/call-macro-in-condition-on-run-time/90434 "2022-11-18T09:59:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brian1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian1/32/49250_2.png) [@Brian1](https://discourse.julialang.org/u/Brian1)\
**Post date:** [November 18, 2022, 9:59am UTC](https://discourse.julialang.org/t/call-macro-in-condition-on-run-time/90434/1 "2022-11-18T09:59:05Z")

</div>

There are the codes:

```julia
struct A{T}
    n::T
end
(a::A{Int})(x::Vector) = begin
    res = similar
    for i = 1 : size(x, 1)
        res[i] = x[i]
    end
    res
end

(a::A{Float64})(x::Vector) = begin
    res = similar
    @threads for i = 1 : size(x, 1)
        res[i] = x[i]
    end
    res
end

```

When `a.n` isa `Int`, I need the loop block runs on main thread,and when `a.n` isa `Float64`, I need that runs on multi threads. But the two methods isa all same except only the `@threads`. Is there a way I can combine the two dispatched methods? Like:

```julia
(a::A{T})(x::Vector) where {T} = begin
    res = similar
    func(a) for i = 1 : size(x, 1)
        res[i] = x[i]
    end
    res
end
func(a::A{Int}) = @inbounds
func(a::A{Float64) = @threads

```

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [November 18, 2022, 10:22am UTC](https://discourse.julialang.org/t/call-macro-in-condition-on-run-time/90434/2 "2022-11-18T10:22:19Z")

</div>

No. Macros are syntax transforms, they happen just after parsing and before the function itself exists. You can’t switch whether a macro is applied or not at runtime, they are always expanded.

If you want to switch between threaded and nonthreaded versions, you need either a runtime `if` to switch between different inlined versions or call different functions like in your first example.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [November 18, 2022, 12:43pm UTC](https://discourse.julialang.org/t/call-macro-in-condition-on-run-time/90434/3 "2022-11-18T12:43:07Z")

</div>

Or you use a generated function that splices in the threading macro if the argument is a Float64. This is almost kind of a “runtime macro” solution because the source code of the generated function is built using the type information received at runtime (but there will be no runtime overhead because compilation happens only once).
