# Overloading zeros

**URL:** <https://discourse.julialang.org/t/overloading-zeros/84540>\
**Category:** General Usage\
**Tags:** struct\
**Created:** [July 20, 2022, 2:31pm UTC](https://discourse.julialang.org/t/overloading-zeros/84540 "2022-07-20T14:31:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [July 20, 2022, 2:31pm UTC](https://discourse.julialang.org/t/overloading-zeros/84540/1 "2022-07-20T14:31:41Z")

</div>

I find the error message here unclear, it seems to me that the `zeros` for this new type is clearly defined. What is going on here? Is there any reason _not_ to define new methods to `zeros` explicitly?

```julia
julia> struct A
           n::Int
           x::Vector{Float64}
       end

julia> Base.zeros(::A, n) = A(0, zeros(n))

julia> zeros(A, 3)
ERROR: MethodError: no method matching zero(::Type{A})

```

---

<div class="post-metadata">

**Author:** ![tomaklutfu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomaklutfu/32/2411_2.png) [@tomaklutfu](https://discourse.julialang.org/u/tomaklutfu)\
**Post date:** [July 20, 2022, 2:33pm UTC](https://discourse.julialang.org/t/overloading-zeros/84540/2 "2022-07-20T14:33:13Z")

</div>

> [@lmiq](#):
>
> `Base.zeros(::A, n) = A(0, zeros(n))`

It should be

`Base.zeros(::Type{A}, n) = A(0, zeros(n))`

The former expects an instance of `A` and the latter expects the type.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [July 20, 2022, 2:39pm UTC](https://discourse.julialang.org/t/overloading-zeros/84540/3 "2022-07-20T14:39:53Z")

</div>

Thanks, forgot about that.
