# How to setup a user defined type to work in arithmetical expressions when the type acts as a vector

**URL:** <https://discourse.julialang.org/t/how-to-setup-a-user-defined-type-to-work-in-arithmetical-expressions-when-the-type-acts-as-a-vector/124101>\
**Category:** General Usage\
**Created:** [December 22, 2024, 6:56pm UTC](https://discourse.julialang.org/t/how-to-setup-a-user-defined-type-to-work-in-arithmetical-expressions-when-the-type-acts-as-a-vector/124101 "2024-12-22T18:56:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Sagan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/david_sagan/32/33792_2.png) [@David\_Sagan](https://discourse.julialang.org/u/David_Sagan)\
**Post date:** [December 22, 2024, 6:56pm UTC](https://discourse.julialang.org/t/how-to-setup-a-user-defined-type-to-work-in-arithmetical-expressions-when-the-type-acts-as-a-vector/124101/1 "2024-12-22T18:56:33Z")

</div>

Setting up a struct to act as a scalar in arithmetical expressions is easy. For example:

```julia
mutable struct D <: Number; v; end
Base.promote_rule(::Type{D}, ::Type{T}) where T = T
Base.convert(::Type{T}, d::D) where T <: Number = T(d.v)
d = D(3.5)
d + 4.7 # Gives 8.2 as expected

```

However, when I try to use a struct like a vector I cannot get it to work. For example I tried:

```julia
mutable struct F <: Number; v; end
Base.promote_rule(::Type{F}, ::Type{T}) where T = T
Base.convert(::Type{T}, f::F) where T <: Number = T.(f.v)
Base.convert(::Type{Vector{T}}, f::F) where T <: Number = T.(f.v)
f = F([3.5, 3.4])
f + [1.1, 2.2] # This gives an error

```

And the last line gives the error:

```julia
ERROR: LoadError: MethodError: no method matching +(::F, ::Vector{Float64})
For element-wise addition, use broadcasting with dot syntax: scalar .+ array
The function `+` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  +(::Any, ::Any, ::Any, ::Any...)
   @ Base operators.jl:596
  +(::Array, ::Array...)
   @ Base arraymath.jl:12
  +(::Number, ::LinearAlgebra.UniformScaling)
   @ LinearAlgebra ~/.julia/juliaup/julia-1.11.2+0.aarch64.apple.darwin14/share/julia/stdlib/v1.11/LinearAlgebra/src/uniformscaling.jl:145

```

Note: Dot syntax does not work here and in any case I want to be able to use the standard notation (no dot needed to add vectors).

What is the best way to solve this?

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [December 24, 2024, 10:33am UTC](https://discourse.julialang.org/t/how-to-setup-a-user-defined-type-to-work-in-arithmetical-expressions-when-the-type-acts-as-a-vector/124101/2 "2024-12-24T10:33:11Z")

</div>

The problem is that `F` subtypes `Number` and there’s no method for `+(::Number, ::Array)`.  
On the other hand, it works with `D` because there is a generic method `+(::Number, ::Number)` which applies promotion and then calls a more specific method.
