# Function working on parametric types

**URL:** <https://discourse.julialang.org/t/function-working-on-parametric-types/93570>\
**Category:** New to Julia\
**Created:** [January 26, 2023, 12:37pm UTC](https://discourse.julialang.org/t/function-working-on-parametric-types/93570 "2023-01-26T12:37:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![weltenbummler](https://avatars.discourse-cdn.com/v4/letter/w/82dd89/32.png) [@weltenbummler](https://discourse.julialang.org/u/weltenbummler)\
**Post date:** [January 26, 2023, 12:37pm UTC](https://discourse.julialang.org/t/function-working-on-parametric-types/93570/1 "2023-01-26T12:37:14Z")

</div>

Hello,

For a parametric type such as

```julia
struct Point{T <: Real}
    x::T
    y::T
end

```

I can define the following three functions:

```julia
coordinates1(p::Point) = (p.x, p.y)
coordinates2(p::Point{<:Real}) = (p.x, p.y)
coordinates3(p::Point{<:Integer}) = (p.x, p.y)

```

I have two questions:

1. The third function would only work for inputs of type `Point{<:Integer}` but what is the difference between the first and the second definition?

2. If I wanted to define a function that works for all `T`, would I rather choose a definition such as `coordinates1` where I drop the specification of the type, or would I go for a definition such as `coordinates2` where I copy the restriction `T<:Real` from the definition of the parametric struct `Point`?

Thank you for your help

Michael

---

<div class="post-metadata">

**Author:** ![filchristou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/filchristou/32/26760_2.png) [@filchristou](https://discourse.julialang.org/u/filchristou)\
**Post date:** [January 26, 2023, 1:48pm UTC](https://discourse.julialang.org/t/function-working-on-parametric-types/93570/2 "2023-01-26T13:48:50Z")

</div>

> [@weltenbummler](#):
>
> 1. …what is the difference between the first and the second definition?

The methods `coordinates1` and `coordinates2` are exactly the same. In fact if you give them the same name:

```julia
julia> coordinates(p::Point) = (p.x, p.y)
coordinates (generic function with 1 method)

julia> coordinates(p::Point{<:Real}) = (p.x, p.y)
coordinates (generic function with 1 method)

julia> methods(coordinates)
# 1 method for generic function "coordinates":
[1] coordinates(p::Point) in Main at REPL[11]:1

```

you will see that in the end only one method is really defined.  
This changes when you introduce the `coordinates3` signature

```julia
julia> coordinates(p::Point{<:Integer}) = (p.x, p.y)
coordinates (generic function with 2 methods)

julia> methods(coordinates)
# 2 methods for generic function "coordinates":
[1] coordinates(p::Point{<:Integer}) in Main at REPL[13]:1
[2] coordinates(p::Point) in Main at REPL[11]:1

```

Ofc you give different names, so in your case all 3 function exist. Just wanted to say that there is no difference in the argument types between the the first and the second.

> [@weltenbummler](#):
>
> 1. If I wanted to define a function that works for all `T`…

Since you defined `Point{T<:Real}` you can only ever define `Point` with `T<:Real`. And as mentioned previously it makes no difference whether you use the first or the second signature. I personally find the first more readable, as there you do not repeat yourself.

---

<div class="post-metadata">

**Author:** ![weltenbummler](https://avatars.discourse-cdn.com/v4/letter/w/82dd89/32.png) [@weltenbummler](https://discourse.julialang.org/u/weltenbummler)\
**Post date:** [January 26, 2023, 4:27pm UTC](https://discourse.julialang.org/t/function-working-on-parametric-types/93570/3 "2023-01-26T16:27:26Z")

</div>

> [@filchristou](#):
>
> Since you defined `Point{T<:Real}` you can only ever define `Point` with `T<:Real`. And as mentioned previously it makes no difference whether you use the first or the second signature. I personally find the first more readable, as there you do not repeat yourself.

Thank you. Yes, this was my preferred option too. Glad to see that it is equivalent to the more verbose signature.

---

<div class="post-metadata">

**Author:** ![suavesito](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/suavesito/32/34386_2.png) [@suavesito](https://discourse.julialang.org/u/suavesito)\
**Post date:** [January 26, 2023, 5:22pm UTC](https://discourse.julialang.org/t/function-working-on-parametric-types/93570/4 "2023-01-26T17:22:03Z")

</div>

Hi @weltenbummler, could you mark the second post as the solution to this thread? This way is easier to find for new people that may have the same question.

---

<div class="post-metadata">

**Author:** ![weltenbummler](https://avatars.discourse-cdn.com/v4/letter/w/82dd89/32.png) [@weltenbummler](https://discourse.julialang.org/u/weltenbummler)\
**Post date:** [January 26, 2023, 8:27pm UTC](https://discourse.julialang.org/t/function-working-on-parametric-types/93570/5 "2023-01-26T20:27:42Z")

</div>

Done; thanks for the reminder.
