I’m going assume getServices
looks something like
let getServices xs = xs |> List.map getService
As far as the type signature of getServices
, it takes a list and returns a list, and the types don’t indicate in any way that the lists have to be the same length. There isn’t really a way in F# to say that a list
has a known length. So you’ve got some options: you could just do it the manual/boring way:
let s1 = getService typeof<web.I18n>
let s2 = getService typeof<DB.DBClient>
// or
let s1, s2 = getService typeof<web.I18n>, getService typeof<DB.DBClient>
You could just “bypass” the warning by not using pattern matching
let s1, s2 =
let services = getServices [typeof<web.I18n>; typeof<DB.DBClient>]
in services[0], services[1]
If you don’t mind adding some extra helper code, oftentimes when you find yourself wanting to have a list of a fixed length, tuples are the right tool to reach for. Unfortunately, some of the functionality around tuples isn’t built-up the same as for lists. In my company’s codebase, we have some extra helper modules for tuples, e.g.,
module Tuple2 =
let map fn (x1, x2) = (fn x1, fn x2)
which gets a fair bit of use. Here, you could do
let (s1, s2) = Tuple2.map getService (typeof<web.I18n>, typeof<DB.DBClient>)