# Using types as parameters of a function

**URL:** <https://discourse.julialang.org/t/using-types-as-parameters-of-a-function/108342>\
**Category:** General Usage\
**Tags:** question\
**Created:** [January 4, 2024, 1:49pm UTC](https://discourse.julialang.org/t/using-types-as-parameters-of-a-function/108342 "2024-01-04T13:49:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jiang\_ming\_zhang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jiang_ming_zhang/32/204063_2.png) [@jiang\_ming\_zhang](https://discourse.julialang.org/u/jiang_ming_zhang)\
**Post date:** [January 4, 2024, 1:49pm UTC](https://discourse.julialang.org/t/using-types-as-parameters-of-a-function/108342/1 "2024-01-04T13:49:22Z")

</div>

We have the following code:

```julia
abstract type MyType end 
struct MyType1 <: MyType end 
myfun(::MyType1, x, args...) = x 

```

How can I call the function ‘myfun’?

```julia
julia> myfun(::MyType1, 2)
ERROR: syntax: invalid "::" syntax around REPL[10]:1
Stacktrace:
 [1] top-level scope
   @ REPL[10]:1

julia> myfun(:MyType1, 2)
ERROR: MethodError: no method matching myfun(::Symbol, ::Int64)

Closest candidates are:
  myfun(::MyType1, ::Any, ::Any...)
   @ Main REPL[5]:1

Stacktrace:
 [1] top-level scope
   @ REPL[11]:1

julia> myfun(MyType1, 2)
ERROR: MethodError: no method matching myfun(::Type{MyType1}, ::Int64)

Closest candidates are:
  myfun(::MyType1, ::Any, ::Any...)
   @ Main REPL[5]:1

Stacktrace:
 [1] top-level scope
   @ REPL[12]:1

```

I am really confused with the ::

---

<div class="post-metadata">

**Author:** ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)\
**Post date:** [January 4, 2024, 1:55pm UTC](https://discourse.julialang.org/t/using-types-as-parameters-of-a-function/108342/2 "2024-01-04T13:55:09Z")

</div>

The double-colon is used to declare the argument type. I am sure you have seen

```julia
f(x::Int) = # some definition

```

which means that `f` accepts an argument of type `Int`.

When a function does not manipulate with an argument and only cares about the argument’s type, you can omit the argument’s name:

```julia
f(::Int) = # something

```

You can still call it as, e.g., `f(3)`.

In your case, you have

```julia
myfun(::MyType1, x, args...)

```

so you need to pass an instance of `MyType1`. For example:

```julia
myfun(MyType1(), 2)

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [January 4, 2024, 7:58pm UTC](https://discourse.julialang.org/t/using-types-as-parameters-of-a-function/108342/3 "2024-01-04T19:58:37Z")

</div>

There’s another way to define the function signature that lets you call it with a type instead of an instance. While

```julia
myfun(::MyType1, x, args...) 

```

takes an instance as the first argument,

```julia
myfun(::Type{MyType1}, x, args...) 

```

takes the type itself, and can be called as

```julia
myfun(MyType1, 2)

```

It’s not always you can trivially instantiate a type, for example `Float64()` does not work, you need to create a specific value. For some types this could be difficult or expensive.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 4, 2024, 8:01pm UTC](https://discourse.julialang.org/t/using-types-as-parameters-of-a-function/108342/4 "2024-01-04T20:01:33Z")

</div>

> [@DNF](#):
>
> `myfun(::Type{MyType1}, x, args...) `

See also [`Type{T}` in the manual](https://docs.julialang.org/en/v1/manual/types/#man-typet-type).
