# How to use dot notation when extending functions for a user-defined type?

**URL:** https://discourse.julialang.org/t/how-to-use-dot-notation-when-extending-functions-for-a-user-defined-type/27638
**Category:** General Usage
**Created:** [August 17, 2019, 12:10am UTC](https://discourse.julialang.org/t/how-to-use-dot-notation-when-extending-functions-for-a-user-defined-type/27638 "2019-08-17T00:10:02Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![kmundnic](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@kmundnic](https://discourse.julialang.org/u/kmundnic)
#### Post date: [August 17, 2019, 12:10am UTC](https://discourse.julialang.org/t/how-to-use-dot-notation-when-extending-functions-for-a-user-defined-type/27638/1 "2019-08-17T00:10:02Z")

</div>

Let’s say that I create a new struct called `MyMatrix`:

```julia
import Base: size, getindex

struct MyMatrix{T} <: AbstractMatrix{T}
    X::Matrix{T}

    function MyMatrix(n::Int)
        new{Float64}(rand(n,n)) # For sake of the example
    end
end

Base.size(X::MyMatrix) = size(X.X)
Base.getindex(X::MyMatrix, inds...) = getindex(X.X, inds...)

```

Now, if I use `-` from Base, the type `MyMatrix` is not preserved (which is expected):

```julia
julia> X = MyMatrix(2)
2×2 MyMatrix{Float64}:
 0.984525 0.843545
 0.862174 0.266042

julia> X .- 1
2×2 Array{Float64,2}:
 -0.0154746 -0.156455
 -0.137826 -0.733958

```

The question is: how can I extend `-` so that it uses `.` notation and preserves `MyMatrix` type? Ideally, I’d want:

```julia
julia> X .- 1
2×2 MyMatrix{Float64,2}:
 -0.0154746 -0.156455
 -0.137826 -0.733958

```

Thanks!

---

<div class="post-metadata">

### Author: ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)
#### Post date: [August 17, 2019, 12:42am UTC](https://discourse.julialang.org/t/how-to-use-dot-notation-when-extending-functions-for-a-user-defined-type/27638/2 "2019-08-17T00:42:17Z")

</div>

if you want your custom type to be broadcastable, you need to implement the broadcast interface:  
[https://docs.julialang.org/en/v1/manual/interfaces/#man-interfaces-broadcasting-1](https://docs.julialang.org/en/v1/manual/interfaces/#man-interfaces-broadcasting-1)

---

<div class="post-metadata">

### Author: ![kmundnic](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@kmundnic](https://discourse.julialang.org/u/kmundnic)
#### Post date: [August 17, 2019, 12:58am UTC](https://discourse.julialang.org/t/how-to-use-dot-notation-when-extending-functions-for-a-user-defined-type/27638/3 "2019-08-17T00:58:21Z")

</div>

Thanks!
