# Calling a function by argument type only

**URL:** <https://discourse.julialang.org/t/calling-a-function-by-argument-type-only/57094>\
**Category:** New to Julia\
**Tags:** type, dispatch\
**Created:** [March 13, 2021, 8:38pm UTC](https://discourse.julialang.org/t/calling-a-function-by-argument-type-only/57094 "2021-03-13T20:38:50Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeffreyesun](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreyesun/32/13379_2.png) [@jeffreyesun](https://discourse.julialang.org/u/jeffreyesun)\
**Post date:** [March 13, 2021, 8:38pm UTC](https://discourse.julialang.org/t/calling-a-function-by-argument-type-only/57094/1 "2021-03-13T20:38:50Z")

</div>

I have seen functions defined without argument values, just types for dispatch as so:

```julia
function foo(::Int, n)
  return Int(n)
end

```

First question: What is this type-only argument style called? I can’t seem to find its documentation.

Second question: Is there a way to call such a function without passing a value for the first argument? In principle, something like `foo(::Int, 4)` could be possible (but I’m not sure if it’s supported or considered somehow unjulian).

Thanks!

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [March 13, 2021, 8:45pm UTC](https://discourse.julialang.org/t/calling-a-function-by-argument-type-only/57094/2 "2021-03-13T20:45:06Z")

</div>

The usage is probably related to what’s called Traits, or Holy Traits, after Tim Holy. Check out [Types · The Julia Language](https://docs.julialang.org/en/v1.7-dev/manual/types/#%22Value-types%22)

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [March 13, 2021, 8:47pm UTC](https://discourse.julialang.org/t/calling-a-function-by-argument-type-only/57094/3 "2021-03-13T20:47:30Z")

</div>

> Is there a way to call such a function without passing a value for the first argument?

If the function takes a “regular” type, as opposed to a type of a type (`Type{X}`), then no. It all depends on the function’s type signature.

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [March 13, 2021, 9:37pm UTC](https://discourse.julialang.org/t/calling-a-function-by-argument-type-only/57094/4 "2021-03-13T21:37:09Z")

</div>

```julia
f(::Int)

```

Just means that you won’t use the value of the argument in your function. It’s otherwise the same thing as `f(x::Int)`. If you want a function that accepts a type object, not an instance, you write `g(T::Type{Int})` (or indeed `g(::Type{int})`).

These are called as `f(3)` and `g(Int)` respectively.

---

<div class="post-metadata">

**Author:** ![jeffreyesun](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreyesun/32/13379_2.png) [@jeffreyesun](https://discourse.julialang.org/u/jeffreyesun)\
**Post date:** [March 13, 2021, 9:39pm UTC](https://discourse.julialang.org/t/calling-a-function-by-argument-type-only/57094/5 "2021-03-13T21:39:26Z")

</div>

Aha, so I’m guessing I need to bite the bullet and refactor a little like,

```julia
function foo(type, n)
  type==Int && return Int(n)
end

function foo(::Int, n)
  return foo(Int, n)
end

```

Thanks! Also thanks to @nsajko for the reference to Holy Traits. I’ve seen references to this before but now might be a good time to dig into it and see if they can help me in my current project.

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [March 13, 2021, 9:49pm UTC](https://discourse.julialang.org/t/calling-a-function-by-argument-type-only/57094/6 "2021-03-13T21:49:26Z")

</div>

@jeffreyesun, I’m not totally sure what is it that you want to do, could you clarify your goal?

For example, I guess that you want to dispatch on some constant, so maybe Enums would be useful instead?

EDIT: also be aware that passing types as arguments is actually discouraged, as described in one paragraph of the manual section I linked to.

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [March 13, 2021, 9:52pm UTC](https://discourse.julialang.org/t/calling-a-function-by-argument-type-only/57094/7 "2021-03-13T21:52:45Z")

</div>

No, dispatch is definitely the way you want to do that, but you dispatch on the type `Type{Int}`, rather than `Int`. Try `typeof(Int)` to see why.

And if you ever do compare `Type`s, you probably want `T <: Int` rather than `T == Int`. It will succeed if `T` `isa` `Int64` too, for instance.

---

<div class="post-metadata">

**Author:** ![thautwarm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thautwarm/32/37760_2.png) [@thautwarm](https://discourse.julialang.org/u/thautwarm)\
**Post date:** [March 13, 2021, 9:54pm UTC](https://discourse.julialang.org/t/calling-a-function-by-argument-type-only/57094/8 "2021-03-13T21:54:36Z")

</div>

If you want to test your program, you’d generate a random value for the first argument.  
I don’t think it a good idea to allow passing in both `Int` itself and its instances.
