# Types as Function Parameters

**URL:** <https://discourse.julialang.org/t/types-as-function-parameters/25944>\
**Category:** General Usage\
**Created:** [July 2, 2019, 10:22pm UTC](https://discourse.julialang.org/t/types-as-function-parameters/25944 "2019-07-02T22:22:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![clinton](https://avatars.discourse-cdn.com/v4/letter/c/7ba0ec/32.png) [@clinton](https://discourse.julialang.org/u/clinton)\
**Post date:** [July 2, 2019, 10:22pm UTC](https://discourse.julialang.org/t/types-as-function-parameters/25944/1 "2019-07-02T22:22:11Z")

</div>

I feel like this is probably somewhere easily found in the documentation, but I haven’t figured out where, so I think I just don’t know the right vocabulary here.

Suppose I wanted to write the following silly function:

```julia
function parsestring(T::Type, s::AbstractString)
  v::Union{Nothing,T} = tryparse(T,s)

  return v
end

parsestring(Int, "3")
parsestring(Float64, "3.4")

```

I’ve found the above is slow. Alternatively I could write:

```julia
function parsestringFloat64(s::AbstractString)
  v::Union{Nothing,Float64} = tryparse(Float64,s)

  return v
end

function parsestringInt(s::AbstractString)
  v::Union{Nothing,Int} = tryparse(Int,s)

  return v
end

parsestringInt("3")
parsestringFloat64("3.4")

```

which is predictably fast. The question is if there is a more idiomatic way of writing and calling functions in the second case? Do I need to dispatch on values?

---

<div class="post-metadata">

**Author:** ![clinton](https://avatars.discourse-cdn.com/v4/letter/c/7ba0ec/32.png) [@clinton](https://discourse.julialang.org/u/clinton)\
**Post date:** [July 2, 2019, 10:58pm UTC](https://discourse.julialang.org/t/types-as-function-parameters/25944/2 "2019-07-02T22:58:17Z")

</div>

I should add that dispatching on values is very fast, although I still feel like I am missing a more idiomatic way to do this:

```julia
function parsestring(s::AbstractString, ::Val{T}) where T
  v::Union{Nothing,T} = tryparse(T,s)

  return v
end

parsestring("3", Val(Int))
parsestring("3.4", Val(Float64))

```

---

<div class="post-metadata">

**Author:** ![hildebrandmw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hildebrandmw/32/3850_2.png) [@hildebrandmw](https://discourse.julialang.org/u/hildebrandmw)\
**Post date:** [July 2, 2019, 11:11pm UTC](https://discourse.julialang.org/t/types-as-function-parameters/25944/3 "2019-07-02T23:11:24Z")

</div>

Hi Clinton!

You can try the following:

```julia
function parsestring(::Type{T}, s::AbstractString) where {T}
    v::Union{Nothing,T} = tryparse(T, s)
    return v
end

parsestring(Int, "3")
parsestring(Float64, "3.4")

```

This is a pattern that shows up fairly often in Julia code.

---

<div class="post-metadata">

**Author:** ![clinton](https://avatars.discourse-cdn.com/v4/letter/c/7ba0ec/32.png) [@clinton](https://discourse.julialang.org/u/clinton)\
**Post date:** [July 2, 2019, 11:25pm UTC](https://discourse.julialang.org/t/types-as-function-parameters/25944/4 "2019-07-02T23:25:54Z")

</div>

Thanks! That was exactly what I was looking for.

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [July 2, 2019, 11:31pm UTC](https://discourse.julialang.org/t/types-as-function-parameters/25944/5 "2019-07-02T23:31:38Z")

</div>

alternatively:

```julia
#general case
function parsestring(::Type{T},s::AbstractString) where T
  v::Union{Nothing,T} = tryparse(T,s) #the return is't necessary
end

#optimized case, for int64 and float64
function parsestring(::Type{Int64},s::AbstractString) 
  v::Union{Nothing,Int64} = tryparse(Int64,s) 
end

function parsestring(::Type{Float64},s::AbstractString) 
  v::Union{Nothing,Float64} = tryparse(Float64,s) 
end

```

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [July 3, 2019, 3:07am UTC](https://discourse.julialang.org/t/types-as-function-parameters/25944/6 "2019-07-03T03:07:13Z")

</div>

The “optimized case” should not be necessary and does not actually affect performance, so there’s no need to go beyond the nice generic `where T` form:

```julia
julia> using BenchmarkTools

julia> function parsestring(::Type{T},s::AbstractString) where T
         v::Union{Nothing,T} = tryparse(T,s) #the return is't necessary
       end
parsestring (generic function with 1 method)

julia> @btime parsestring($Int64, $("1"))
  18.972 ns (0 allocations: 0 bytes)
1

julia> function parsestring(::Type{Int64},s::AbstractString) 
         v::Union{Nothing,Int64} = tryparse(Int64,s) 
       end
parsestring (generic function with 2 methods)

julia> @btime parsestring($Int64, $("1"))
  18.974 ns (0 allocations: 0 bytes)
1

```

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [July 3, 2019, 3:26am UTC](https://discourse.julialang.org/t/types-as-function-parameters/25944/7 "2019-07-03T03:26:19Z")

</div>

Lol, I forgot the interpolation when measuring 😅
