# Operations on expressions

**URL:** <https://discourse.julialang.org/t/operations-on-expressions/13666>\
**Category:** Internals & Design\
**Created:** [August 18, 2018, 3:24pm UTC](https://discourse.julialang.org/t/operations-on-expressions/13666 "2018-08-18T15:24:37Z")\
**Posts on this page:** 1\
**Showing post:** 12

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [August 26, 2018, 9:59pm UTC](https://discourse.julialang.org/t/operations-on-expressions/13666/12 "2018-08-26T21:59:45Z")

</div>

The principle which all `@force` users must keep in mind is this: the goal is to extend `Base` methods locally without affecting the global method table by type piracy, and to be able to import them into another package to have the same local effect. In order to avoid the type piracy, one must define a new local complementary n-ary method that will fall back on the base method with `Any` arguments. Then there will be a tiered alternative dispatch layer within the local package scope that redirects to the default `Base` dispatch generically. This has many advantages, including a 2x-improvement in precompile time (since the precompilation is now tiered), which actually makes a big difference if this technique is applied to a large number of operations (on the order of 50-100 in `Reduce`).

```nohighlight
module ExtendedPackage
+(x...) = Base.:+(x...)
+(x::Symbol,y::Number...) = Expr(:call,:+,x,y...)
end

```

Then you can use it locally with the `@force` macro, which automatically forces imports of all exported methods one by one;

```nohighlight
module NewScope
using ForceImport
@force using ExtendedPackage
end

```

You will now have the property

```nohighlight
julia> Base.:+ == NewScope.:+
false

```

where the `+` in the `NewScope` is now the extended plus that falls back on `Base`, which is also different from the `+` in `Main`. All it takes is the application of this principle, which has been pioneered with the development of the `Reduce.Algebra` module (defined in [src/args.jl](https://github.com/chakravala/Reduce.jl/blob/master/src/args.jl) and [src/unary.jl](https://github.com/chakravala/Reduce.jl/blob/master/src/unary.jl)).

> <https://github.com/chakravala/Reduce.jl/blob/382b5ff84bb66067f2f087156c6bd930c297318a/src/Reduce.jl#L105-L123>

The main difficulty lies in properly designing the redirection from the tiered method dispatch so that the infix operations work naturally with a variety of syntax.

---

_[View the full topic](https://discourse.julialang.org/t/operations-on-expressions/13666)._
