# Passing abstract structs to a function

**URL:** <https://discourse.julialang.org/t/passing-abstract-structs-to-a-function/99877>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [June 5, 2023, 9:03am UTC](https://discourse.julialang.org/t/passing-abstract-structs-to-a-function/99877 "2023-06-05T09:03:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Santos\_Michelena](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/santos_michelena/32/50502_2.png) [@Santos\_Michelena](https://discourse.julialang.org/u/Santos_Michelena)\
**Post date:** [June 5, 2023, 9:03am UTC](https://discourse.julialang.org/t/passing-abstract-structs-to-a-function/99877/1 "2023-06-05T09:03:36Z")

</div>

Hello, I’m trying to write a solver library for a very specific machine learning problem and I have a function that errors in runtime and I really don’t understand what exactly is wrong.

In particular, the function that errors is:

```julia
function risk(S::Vector{Sample}, γ::Vector{Function})
	return ( S .|> (s -> loss(s, γ)) |> sum ) / size(S)[1]
end

```

Where:

```julia
function loss(s::Sample, γ::Vector{Function})
	v = collect(Iterators.flatten([g(y) for (g, y) in zip(γ, s.Y)]))
	return real(dot(v, s.R * v) + 2 * real(dot(v, s.r)) + s.c)
end

```

And Sample is defined as:

```julia
struct Sample{T <: Number}
	Y::Vector{Vector{T}}
	R::Matrix{T}
	r::Vector{T}
	c::T
end

```

The error I get reads:

```julia
MethodError: no method matching risk(::Vector{Sample{Float64}}, ::Vector{Function})

Closest candidates are:
  risk(!Matched::Vector{Sample}, ::Vector{Function})

```

This doesn’t make sense to me, as Vector{Sample{Float64}} is a subtype of Vector{Sample}. Moreover calling `loss` on a single sample works just fine. Additionally, calling risk(Vector{Sample}(S), \gamma} also works, which is honestly baffling, as I am essentially casting to a more generic type.

Also, I am declaring Sample as parametrised by a `Number` since I need to handle both real and complex valued samples.

I feel that intuitively, there’s nothing wrong with my code, after all, Julia main advertised feature is multiple dispatch, i.e the ability to write generic functions and structs. But then again, I am pretty new to the language, so I must be missing a piece of the puzzle here.

---

<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:** [June 5, 2023, 9:21am UTC](https://discourse.julialang.org/t/passing-abstract-structs-to-a-function/99877/2 "2023-06-05T09:21:43Z")

</div>

> [@Santos\_Michelena](#):
>
> as Vector{Sample{Float64}} is a subtype of Vector{Sample}

It is not (see [documentation](https://docs.julialang.org/en/v1/manual/types/#man-parametric-composite-types) and [other](https://discourse.julialang.org/t/whats-with-the-invariant-parametric-types-gotcha/55090) [discussions](https://discourse.julialang.org/t/why-1-2-3-is-not-a-vector-number/52645) [here](https://discourse.julialang.org/t/subtyping-conventions-for-dicts-are-unexpected/33040)).  
You need to explicitly specify the type as `Vector{<:Sample}` (and maybe the second type as `Vector{<:Function}`).

---

<div class="post-metadata">

**Author:** ![Santos\_Michelena](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/santos_michelena/32/50502_2.png) [@Santos\_Michelena](https://discourse.julialang.org/u/Santos_Michelena)\
**Post date:** [June 5, 2023, 9:54am UTC](https://discourse.julialang.org/t/passing-abstract-structs-to-a-function/99877/3 "2023-06-05T09:54:59Z")

</div>

Indeed, this solves the issue.  
Thanks a lot.
