# Issue with add function for genie webframe for adding numbers

**URL:** <https://discourse.julialang.org/t/issue-with-add-function-for-genie-webframe-for-adding-numbers/119171>\
**Category:** New to Julia\
**Created:** [September 7, 2024, 4:19pm UTC](https://discourse.julialang.org/t/issue-with-add-function-for-genie-webframe-for-adding-numbers/119171 "2024-09-07T16:19:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shiva\_raj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shiva_raj/32/50626_2.png) [@Shiva\_raj](https://discourse.julialang.org/u/Shiva_raj)\
**Post date:** [September 7, 2024, 4:19pm UTC](https://discourse.julialang.org/t/issue-with-add-function-for-genie-webframe-for-adding-numbers/119171/1 "2024-09-07T16:19:59Z")

</div>

Hi Team,  
I have been doing some test for a school project for a math’s function, Need help I am trying to create a function for adding numbers with steps returned as JSON output.

```julia
using Genie, Genie.Router, Genie.Renderer.Json, Genie.Requests
include("addfun.jl")

# Define the route for the POST request
route("/addstep", method = POST) do
    # Parse JSON payload
    payload = Genie.Requests.jsonpayload()
    
    # Extract the list of numbers
    numbers = payload["numbers"]
    
    # Perform the addition (sum of the numbers)
    result = add_and_display(numbers)

    # Return the result as JSON
    return Json.json(Dict("result" => result))
end

```

the below add function

```julia
using JSON

function add_and_display(numbers::Vector{<:Union{Int, Float64}})
    # Initialize variables to store steps
    steps = []
    
    # Step 1: Sum the numbers
    sum_numbers = sum(numbers)
    
    # Step 2: Store each number
    push!(steps, "Numbers:")
    for num in numbers
        push!(steps, string(num))
    end
    
    # Step 3: Store the sum
    push!(steps, "\nSum:")
    push!(steps, string(sum_numbers))
    
    # Step 4: Store the solution steps
    push!(steps, "\nSolution Steps:")
    for i in 1:length(numbers)-1
        step_description = "Step $i: $(sum(numbers[1:i])) + $(numbers[i+1]) = $(sum(numbers[1:i+1]))"
        push!(steps, step_description)
    end
    
    # Return the steps and result as a dictionary for JSON serialization
    result = Dict("sum" => sum_numbers, "steps" => steps)
    
    return result
end

```

for above I am getting error.

```julia

julia> ┌ Error: MethodError: no method matching add_and_display(::JSON3.Array{Int64, Base.CodeUnits{UInt8, String}, SubArray{UInt64, 1, Vector{UInt64}, Tuple{UnitRange{Int64}}, true}})
│
│ Closest candidates are:
│ add_and_display(!Matched::Vector{Int64})
│ @ Main /home/shiva/gitcode/Maths-project/module/Genie/addfun.jl:3
│ add_and_display(!Matched::Vector{<:Union{Float64, Int64}})
│ @ Main /home/shiva/gitcode/Maths-project/module/Genie/addfun.jl:3

```

---

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [September 8, 2024, 10:13am UTC](https://discourse.julialang.org/t/issue-with-add-function-for-genie-webframe-for-adding-numbers/119171/2 "2024-09-08T10:13:00Z")

</div>

That is the same problem you already had before, namely that the type on your method is too narrow:

```julia-repl
julia> using JSON3

julia> nums = JSON3.read("[1, 2, 3]")
3-element JSON3.Array{Int64, Base.CodeUnits{UInt8, String}, Vector{UInt64}}:
 1
 2
 3

julia> nums isa Vector{<:Union{Int, Float64}}
false

# Probably the right type capturing what you want -- vector-like collection of numbers
julia> nums isa AbstractVector{<:Real}
true

# Collect often changes type
julia> collect(nums) isa Vector{<:Union{Int, Float64}}
true

```

There are many different types, e.g., views, subvectors etc, that behave like vectors in Julia, but are not of type `Vector`.  
For prototyping, I suggest to just leave out type signatures altogether in Julia (just like you would in Python or other dynamic languages) or try to use the most general type possible, e.g., `AbstractVector`. Except for OOP like interfaces, where methods need to be implemented for each concrete type, many methods in Julia should be defined for abstract types, i.e., specifying what behavioural interface to expect, but not the concrete type.

---

<div class="post-metadata">

**Author:** ![Shiva\_raj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shiva_raj/32/50626_2.png) [@Shiva\_raj](https://discourse.julialang.org/u/Shiva_raj)\
**Post date:** [September 10, 2024, 1:09pm UTC](https://discourse.julialang.org/t/issue-with-add-function-for-genie-webframe-for-adding-numbers/119171/3 "2024-09-10T13:09:19Z")

</div>

> [@bertschi](#):
>
> `AbstractVector{<:Real}`

Hi your solution worked.  
Thanks
