Issue with add function for genie webframe for adding numbers

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.

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

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> ┌ 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

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

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.

Hi your solution worked.
Thanks