# Overloading a function specifically for symbolics

**URL:** https://discourse.julialang.org/t/overloading-a-function-specifically-for-symbolics/85726
**Category:** General Usage
**Tags:** symbolics
**Created:** [August 14, 2022, 2:04pm UTC](https://discourse.julialang.org/t/overloading-a-function-specifically-for-symbolics/85726 "2022-08-14T14:04:33Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![user664303](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/user664303/32/37843_2.png) [@user664303](https://discourse.julialang.org/u/user664303)
#### Post date: [August 14, 2022, 2:04pm UTC](https://discourse.julialang.org/t/overloading-a-function-specifically-for-symbolics/85726/1 "2022-08-14T14:04:33Z")

</div>

The type of a symbolic is Num:

```julia
julia> using Symbolics
julia> @variables x
julia> typeof(x)
Num

```

How can I write a function specifically for symbolics? Let’s say there is already a `genericfun(x::Num, y::Num)` defined in Base or some standard library package. How could I overload this function solely for symbolics?

---

<div class="post-metadata">

### Author: ![RobertGregg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robertgregg/32/22105_2.png) [@RobertGregg](https://discourse.julialang.org/u/RobertGregg)
#### Post date: [August 14, 2022, 4:24pm UTC](https://discourse.julialang.org/t/overloading-a-function-specifically-for-symbolics/85726/2 "2022-08-14T16:24:13Z")

</div>

I’m not sure what you mean by symbolic variables. Do you mean you want the function to accept a [Symbol](https://docs.julialang.org/en/v1/manual/metaprogramming/#Symbols)? For example, you can multiply two numbers, and use multiply to concatenate two strings, but there is no method for Symbols. However we can define one by importing the function:

```julia
julia> :h * :ello
ERROR: MethodError: no method matching *(::Symbol, ::Symbol)

julia> import Base.*

julia> *(x::Symbol, y::Symbol) = Symbol(String(x) * String(y))        
* (generic function with 843 methods)

julia> :h * :ello
:hello

```

Or are you more thinking in the context of computer algebra systems (CAS)? For that you can use [Symbolics.jl](https://symbolics.juliasymbolics.org/stable/tutorials/symbolic_functions/#Registering-Functions-1).

---

<div class="post-metadata">

### Author: ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)
#### Post date: [August 15, 2022, 8:08am UTC](https://discourse.julialang.org/t/overloading-a-function-specifically-for-symbolics/85726/3 "2022-08-15T08:08:16Z")

</div>

Can you describe in more detail what you’re trying to do?

If you’re talking about `Num` from Symbolics.jl, this would be considered [type piracy](https://docs.julialang.org/en/v1/manual/style-guide/#Avoid-type-piracy), since neither the type `Num` nor the function `genericfun` are originally from your code. Type piracy is usually a bad idea and can lead to unintended side effects.

If you think it makes sense for `genericfun` to be defined for `Num` types, maybe that’s worth a PR to Symbolics.jl.

If not, depending on what you’re trying to accomplish, you might want to either use a different function name (the easier option) or create a wrapper type of your own around `Num`.

---

<div class="post-metadata">

### Author: ![user664303](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/user664303/32/37843_2.png) [@user664303](https://discourse.julialang.org/u/user664303)
#### Post date: [August 15, 2022, 8:59am UTC](https://discourse.julialang.org/t/overloading-a-function-specifically-for-symbolics/85726/4 "2022-08-15T08:59:36Z")

</div>

Sorry, my question wasn’t clear enough. I mean a symbolic, defined in the Symbolics package. I edited the question to reflect this.

---

<div class="post-metadata">

### Author: ![user664303](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/user664303/32/37843_2.png) [@user664303](https://discourse.julialang.org/u/user664303)
#### Post date: [August 15, 2022, 9:02am UTC](https://discourse.julialang.org/t/overloading-a-function-specifically-for-symbolics/85726/5 "2022-08-15T09:02:47Z")

</div>

If I were to do what I’m asking, I would indeed do it inside the Symbolics package and open a PR. For now it’s simply an academic question. I don’t understand how the package can specialise on symbolic values only, if they’re defined to be Num, which is a very general type.

---

<div class="post-metadata">

### Author: ![user664303](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/user664303/32/37843_2.png) [@user664303](https://discourse.julialang.org/u/user664303)
#### Post date: [August 15, 2022, 9:07am UTC](https://discourse.julialang.org/t/overloading-a-function-specifically-for-symbolics/85726/6 "2022-08-15T09:07:05Z")

</div>

So I’ve realised that I was confusing the type `Num` (actually defined in the Symbolics package) with the generic type `Number`. So in fact I can just specialise on `Num`.
