HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Moduleservant-server-0.20.2Haskell2010

Servant.Server.Internal.Context

  • 2 types
  • 1 class
  • 2 values
datadata Context (contextTypes :: [Type]) where
#

Contexts are used to pass values to combinators. (They are not meant to be used to pass parameters to your handlers, i.e. they should not replace any custom ReaderT-monad-stack that you're using with hoistServer.) If you don't use combinators that require any context entries, you can just use serve as always.

If you are using combinators that require a non-empty Context you have to use serveWithContext and pass it a Context that contains all the values your combinators need. A Context is essentially a heterogeneous list and accessing the elements is being done by type (see getContextEntry). The parameter of the type Context is a type-level list reflecting the types of the contained context entries. To create a Context with entries, use the operator (:.):

Example1 expression
:type True :. () :. EmptyContextTrue :. () :. EmptyContext :: Context '[Bool, ()]

Constructors

Instances4Eq, Show
  • Eq (Context '[])Defined in servant-server-0.20.2 · Servant.Server.Internal.Context
  • (Eq a, Eq (Context as)) => Eq (Context (a ': as))Defined in servant-server-0.20.2 · Servant.Server.Internal.Context
  • Show (Context '[])Defined in servant-server-0.20.2 · Servant.Server.Internal.Context
  • (Show a, Show (Context as)) => Show (Context (a ': as))Defined in servant-server-0.20.2 · Servant.Server.Internal.Context
familytype family (.++) (l1 :: [Type]) (l2 :: [Type]) :: [Type] where
#

Append two type-level lists.

Hint: import it as

import Servant.Server (type (.++))

Equations

  • (.++) '[] a = a
  • (.++) (a ': as) b = a ': as .++ b
classclass HasContextEntry (context :: [Type]) val where
#

This class is used to access context entries in Contexts. getContextEntry returns the first value where the type matches:

Example1 expression
getContextEntry (True :. False :. EmptyContext) :: BoolTrue

If the Context does not contain an entry of the requested type, you'll get an error:

Example1 expression
getContextEntry (True :. False :. EmptyContext) :: String......No instance for ...HasContextEntry '[] [Char]......

Methods

Instances2HasContextEntry

support for named subcontexts

2 declarations
datadata NamedContext (name :: Symbol) (subContext :: [Type])
#

Normally context entries are accessed by their types. In case you need to have multiple values of the same type in your Context and need to access them, we provide NamedContext. You can think of it as sub-namespaces for Contexts.

Constructors

valuedescendIntoNamedContext
  1. :: HasContextEntry context (NamedContext name subContext)
  2. => Proxy name
  3. -> Context context
  4. -> Context subContext
#

descendIntoNamedContext allows you to access NamedContexts. Usually you won't have to use it yourself but instead use a combinator like WithNamedContext.

This is how descendIntoNamedContext works:

Example6 expressions
:set -XFlexibleContextslet subContext = True :. EmptyContext:type subContextsubContext :: Context '[Bool]let parentContext = False :. (NamedContext subContext :: NamedContext "subContext" '[Bool]) :. EmptyContext:type parentContextparentContext :: Context '[Bool, NamedContext "subContext" '[Bool]]descendIntoNamedContext (Proxy :: Proxy "subContext") parentContext :: Context '[Bool]True :. EmptyContext