I am using the current pattern of code a lot in a program I am currently writing:
let test_titles = ["a_fault"; "b_fault"; "c_fault"]
let tests: (unit -> 'component option) list = [a_fault; b_fault; c_fault]
let test_registry = List.zip test_titles tests
let apply_test (title, test) = test () |> Option.map (fun s -> (title, s))
let result: (string * 'component) option = test_registry |> List.tryPick apply_test
to have a test registry of tests that identify faulty components and the error type which happens to be the same name as the name of the function.
-
Is there a better way to create this test_registry, preferably without me writing the test names manually (dynamically getting the function name)?
-
Generally, is this idiomatic F#?
Edit: The code had a mistake in the last line. The result is computed using test_registry rather than tests.
One way to avoid the need to write the name of the test explicitly in the code (as a string) would be to use quotations. Instead of creating a list of functions and a list of strings, you can create a list of “quoted” function values. Then you can write code that processes the quotations and gives you everything you need.
I assume that your tests look roughly as below (functions taking unit and returning some value as a result). The list would be constructed as follows:
Then you can write code like this to get information about test:
Here is an example how you would use that:
Alternatively you could modify the line
(*)to produce a function that returns the name of the test and the result, which removes the need forOption.map: