# Why is this function expecting a string instead of accepting an integer?

**URL:** <https://discourse.julialang.org/t/why-is-this-function-expecting-a-string-instead-of-accepting-an-integer/67766>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [September 6, 2021, 5:20pm UTC](https://discourse.julialang.org/t/why-is-this-function-expecting-a-string-instead-of-accepting-an-integer/67766 "2021-09-06T17:20:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![EricChameroy](https://avatars.discourse-cdn.com/v4/letter/e/7ab992/32.png) [@EricChameroy](https://discourse.julialang.org/u/EricChameroy)\
**Post date:** [September 6, 2021, 5:20pm UTC](https://discourse.julialang.org/t/why-is-this-function-expecting-a-string-instead-of-accepting-an-integer/67766/1 "2021-09-06T17:20:09Z")

</div>

Hi everyone. I have this simple function here that doesn’t behave the way I’m expecting:

```julia
function build_person(first_name, last_name, age=nothing)
    # Return a dictionary of information about a person.
    person = Dict("first" => first_name, "last" => last_name)
    if !isnothing(age)
        person["age"] = age
    end
    return person
end

```

When I run this code, passing string values for both `first_name` and `last_name` and an integer for `age`, I get the following error:

```julia
ERROR: LoadError: MethodError: Cannot `convert` an object of type Int64 to an object of type String
Closest candidates are:
  convert(::Type{String}, ::String) at essentials.jl:210
  convert(::Type{T}, ::T) where T<:AbstractString at strings/basic.jl:231
  convert(::Type{T}, ::AbstractString) where T<:AbstractString at strings/basic.jl:232
  ...
Stacktrace:
 [1] setindex!(h::Dict{String, String}, v0::Int64, key::String)
   @ Base .\dict.jl:382
 [2] build_person(first_name::String, last_name::String, age::Int64)
   @ Main d:\#####\person.jl:5
 [3] top-level scope
   @ d:\#####\person.jl:10
in expression starting at d:\#####\person.jl:10

```

It appears that the function is expecting `age` to receive a string value, but I don’t understand why.

---

<div class="post-metadata">

**Author:** ![ianshmean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ianshmean/32/216042_2.png) [@ianshmean](https://discourse.julialang.org/u/ianshmean)\
**Post date:** [September 6, 2021, 5:29pm UTC](https://discourse.julialang.org/t/why-is-this-function-expecting-a-string-instead-of-accepting-an-integer/67766/2 "2021-09-06T17:29:20Z")

</div>

The Dict constructor is assuming you will always use a String key and String value because that’s all you pass it. I.e. it creates a `Dict{String,String}`.

Replace `Dict` with either `Dict{String,Any}` or `Dict{String,Union{String,Int}}` depending on how narrow you can be with types on the dict values

---

<div class="post-metadata">

**Author:** ![EricChameroy](https://avatars.discourse-cdn.com/v4/letter/e/7ab992/32.png) [@EricChameroy](https://discourse.julialang.org/u/EricChameroy)\
**Post date:** [September 6, 2021, 7:02pm UTC](https://discourse.julialang.org/t/why-is-this-function-expecting-a-string-instead-of-accepting-an-integer/67766/3 "2021-09-06T19:02:54Z")

</div>

Thank you! So, `Dict{String, String}` is the default value and if I want to use any other types, then I need to specify which. This helps a lot.

Thanks again!

---

<div class="post-metadata">

**Author:** ![ianshmean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ianshmean/32/216042_2.png) [@ianshmean](https://discourse.julialang.org/u/ianshmean)\
**Post date:** [September 6, 2021, 7:04pm UTC](https://discourse.julialang.org/t/why-is-this-function-expecting-a-string-instead-of-accepting-an-integer/67766/4 "2021-09-06T19:04:30Z")

</div>

Not quite. It’s not the default, it’s just that all the keys and values you give to the initial constructor are strings. That’s when the dict is created and types defined

---

<div class="post-metadata">

**Author:** ![EricChameroy](https://avatars.discourse-cdn.com/v4/letter/e/7ab992/32.png) [@EricChameroy](https://discourse.julialang.org/u/EricChameroy)\
**Post date:** [September 6, 2021, 7:09pm UTC](https://discourse.julialang.org/t/why-is-this-function-expecting-a-string-instead-of-accepting-an-integer/67766/5 "2021-09-06T19:09:49Z")

</div>

So, because the first key-value pairs I passed were `String => String` the dictionary was initialized as `Dict{String, String}`. Therefore, when I tried adding another pair, the dictionary was looking for another `String => String` and when it wasn’t, it threw an error. That makes sense.

Thanks again!

---

<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:** [September 6, 2021, 8:42pm UTC](https://discourse.julialang.org/t/why-is-this-function-expecting-a-string-instead-of-accepting-an-integer/67766/6 "2021-09-06T20:42:58Z")

</div>

This looks like you want a struct:

```julia
struct Person
    firstname::String
    lastname::String
    age::Int64
end

introduce(p::Person) = println("Hi! I'm ", p.firstname, " ", p.lastname, ", and I'm ", p.age, " years old.")

person = Person("Gustav", "Vasa", 64)
introduce(person)

```

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [September 6, 2021, 8:59pm UTC](https://discourse.julialang.org/t/why-is-this-function-expecting-a-string-instead-of-accepting-an-integer/67766/7 "2021-09-06T20:59:26Z")

</div>

By the way, if you are only going to use a small set of keys and don’t expect modifications of the dictionary by user, it is a good idea to use `Symbol`s instead of strings, i.e.

```julia
person = Dict(:first => first_name, :last => last_name)

```

Symbols are basically strings that are not supposed to be subdivided into characters. Because they are “atomic”, the operations with them are faster than with strings. As a bonus, an iterable producing symbol-value pair can be unpacked into function keyword arguments. So, if you have a function

```julia
foo(; first, last, age=nothing) = stuff()

```

you can create a person dict and pass it like

```julia
foo(; person...) # note the semicolon to unpack as kwargs

```
