Improve error messages of implicit parameter constraints
In IHP we're passing lot's of state around using implicit parameters. Often times when users are extracting out functionality into their own functions, they'll get an error because some implicit parameter constraint is missing. It would improve the UX a lot when we could suggest the user to add the constraint to the type signature like below.
Also it might be good to specifically mention the word implicit parameter
because some people wonder what a variable with ?
in front is about, but it's not very google friendly. When we have implicit parameter
inside the error message it might be easier to learn about implicit parameters and get a better understanding.
Given this code:
fetchCategories :: (?modelContext :: ModelContext) => IO ()
fetchCategories = do
categories :: [Category] <- query @Category |> fetch
putContext categories
GHC errors with:
Web/FrontController.hs:18:3: error:
* Could not deduce: ?context::ControllerContext
arising from a use of `putContext'
from the context: ?modelContext::ModelContext
bound by the type signature for:
fetchCategories :: (?modelContext::ModelContext) => IO ()
at Web/FrontController.hs:15:1-59
* In a stmt of a 'do' block: putContext categories
In the expression:
do categories :: [Category] <- query @Category |> fetch
putContext categories
In an equation for `fetchCategories':
fetchCategories
= do categories :: [Category] <- query @Category |> fetch
putContext categories
|
18 | putContext categories
A better error message would be:
Web/FrontController.hs:18:3: error:
* The call to `putContext` requires an implicit parameter `?context::ControllerContext` to be available. Change the type signature to this: fetchCategories :: (?modelContext::ModelContext, ?context :: ControllerContext) => IO ()
at Web/FrontController.hs:15:1-59
* In a stmt of a 'do' block: putContext categories
In the expression:
do categories :: [Category] <- query @Category |> fetch
putContext categories
In an equation for `fetchCategories':
fetchCategories
= do categories :: [Category] <- query @Category |> fetch
putContext categories
|
18 | putContext categories