# Overload zero for a parametric type

**URL:** <https://discourse.julialang.org/t/overload-zero-for-a-parametric-type/11991>\
**Category:** General Usage\
**Tags:** question, parametric-types\
**Created:** [June 27, 2018, 8:38am UTC](https://discourse.julialang.org/t/overload-zero-for-a-parametric-type/11991 "2018-06-27T08:38:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![stakaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stakaz/32/4740_2.png) [@stakaz](https://discourse.julialang.org/u/stakaz)\
**Post date:** [June 27, 2018, 8:38am UTC](https://discourse.julialang.org/t/overload-zero-for-a-parametric-type/11991/1 "2018-06-27T08:38:45Z")

</div>

Hello, I would like to add a zero function for a custom parametric type.

This code for example

```julia
struct Point{T}
	p::T 
end
Point{T}() where T = Point(zero(T))
Point() = Point(0)

```

can produce “zero” elements with

```julia
Point()
Point{Float64}()

```

But when I try to define

```julia
Base.zero(::Point{T}) where T = Point{T}(zero(T))

```

and call it with `zero(Point{Int64})`

I get the following error:

```julia
ERROR: LoadError: MethodError: no method matching zero(::Type{Point{Int64}})
Closest candidates are:
  zero(::Type{Base.LibGit2.GitHash}) at libgit2/oid.jl:106
  zero(::Type{Base.Pkg.Resolve.VersionWeights.VWPreBuildItem}) at pkg/resolve/versionweight.jl:82
  zero(::Type{Base.Pkg.Resolve.VersionWeights.VWPreBuild}) at pkg/resolve/versionweight.jl:124

```

Why can’t julia dispatch to the right method is this case?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [June 27, 2018, 8:48am UTC](https://discourse.julialang.org/t/overload-zero-for-a-parametric-type/11991/2 "2018-06-27T08:48:56Z")

</div>

Look at the error message:

> [@stakaz](#):
>
> zero(::Type{Point{Int64}})

Just define a method

```julia
Base.zero(::Type{Point{T}}) where T = Point{T}(zero(T))

```

for the _type_.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [June 27, 2018, 8:49am UTC](https://discourse.julialang.org/t/overload-zero-for-a-parametric-type/11991/3 "2018-06-27T08:49:12Z")

</div>

You need ::Type{Point{T}}

---

<div class="post-metadata">

**Author:** ![stakaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stakaz/32/4740_2.png) [@stakaz](https://discourse.julialang.org/u/stakaz)\
**Post date:** [June 27, 2018, 8:50am UTC](https://discourse.julialang.org/t/overload-zero-for-a-parametric-type/11991/4 "2018-06-27T08:50:29Z")

</div>

Thanks, I see, I thought, just `::` in front is enough to tell that it is a type.

---

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [June 27, 2018, 9:42am UTC](https://discourse.julialang.org/t/overload-zero-for-a-parametric-type/11991/5 "2018-06-27T09:42:18Z")

</div>

No it is unrelated `f(::SomeThing)` is just a short hand `f(unused_argument::SomeThing)`. So `f(::Point)` is short for `f(unused_point_instance::Point)`, while `f(::Type{Point})` is short for `f(WeAlreadyKnowThisIsPoint::Type{Point})`
