Skip to content

Language Extensions

This chapter has a quick summary of some of the advanced extensions that WebGear makes use of. The GHC User's Guide has more information on these extensions.

Arrows

This extension enables the proc notation for arrows. The arrows tutorial explains this notation in detail.

DataKinds

This extension promotes data types to kind level and enables literals at type level. For example, the QueryParam trait is defined as:

import GHC.TypeLits (Symbol)

data Existence = Required | Optional

data ParseStyle = Strict | Lenient

data QueryParam (e :: Existence) (p :: ParseStyle) (name :: Symbol) (val :: Type) = QueryParam

This lets us write types such as QueryParam Required Strict "offset" Integer. The data constructors Required and Strict got promoted to a type, and the types Existence and ParseStyle got promoted to kinds. This also allows string literals such as "offset" as a type.

This extension also allows using lists at the type level. For example, types such as Response ``With`` [RequiredResponseHeader "Content-Type" Text, Body JSON ErrorResponse] is possible because of this extension.

TypeApplications

WebGear uses this extension to explicitly mention values of type variables in some cases. For example, to extract a trait attribute from a witnessed request:

let maybeAuthor :: Maybe Text
    maybeAuthor = pick @(OptionalQueryParam "author" Text) $ from request

The @(OptionalQueryParam "author" Text) type application specifies the trait to retrieve.

QuasiQuotes

This extension enables the Template Haskell quasi-quotation syntax. WebGear uses this syntax to enable routing middlewares such as route and match. Their usage is described in the Routing chapter.

Usage of this extension is optional. You can implement equivalent functionality without using Template Haskell at the cost of some verbose code.