I probably have all the Record extensions enabled at this point, and was hoping for some smooth sailing if using Records extensively, yet I have hit some headscratchers.
To ilustrate, I have the following definitions
makeRequest :: String -> Request
makeRequest url = Request { method = "GET", url, headers = [] }
data Request = Request {
method :: String,
url :: String,
headers :: [(String,String)]
} deriving stock (Show)
data Response a = Response {
status :: {-# UNPACK #-}!(Int, ByteString),
headers :: Network.HTTP.Types.ResponseHeaders,
body :: a
}
And when I load this up into GHCi, and play around I get into the following two scenarios.
> r = makeRequest "https://example.com"
> r { headers = ("lorem","ipsum") : r.headers }
<interactive>:3:1: error:
ā¢ Record update is ambiguous, and requires a type signature
ā¢ In the expression: r {headers = ("lorem", "ipsum") : r.headers}
In an equation for āitā:
it = r {headers = ("lorem", "ipsum") : r.headers}
> r { headers = ("lorem","ipsum") : r.headers } :: Request
<interactive>:4:5: warning: [-Wambiguous-fields]
The record update r {headers = ("lorem", "ipsum")
: r.headers} with type Request is ambiguous.
This will not be supported by -XDuplicateRecordFields in future releases of GHC.
<interactive>:4:5: warning: [-Wambiguous-fields]
The record update r {headers = ("lorem", "ipsum")
: r.headers} with type Request is ambiguous.
This will not be supported by -XDuplicateRecordFields in future releases of GHC.
Request {method = "GET", url = "https://example.com", headers = [("lorem","ipsum")]}
I know that Response a
and Request
data types share the same record field headers
, but how come GHCi doesnāt āseeā the context in which header
is used given that r
is a concrete type?
Second, given the deprecation warning, how should I approach use-cases like mine going forward? I like the ergonomics of the sample code Iāve written, but seems like itās not in line with the direction Haskell is taking.