Chapter 6. The Yesod Typeclass

Every one of our Yesod applications requires an instance of the Yesod typeclass. So far, we’ve just relied on default implementations of the methods of the Yesod typeclass. In this chapter, we’ll explore the meaning of many of these methods Yesod typeclass.

The Yesod typeclass gives us a central place for defining settings for our application. Everything has a default definition, which is often the right thing. But in order to build a powerful, customized application, you’ll usually end up wanting to override at least a few of these methods.

Note

A common question I hear is, “Why use a typeclass instead of a record type?” There are two main advantages:

  • The methods of the Yesod typeclass may wish to call other methods. With typeclasses, this kind of usage is trivial. It becomes slightly more complicated with a record type.

  • Simplicity of syntax. We want to provide default implementations and allow users to override just the necessary functionality. Typeclasses make this both easy and syntactically nice. Records have a slightly larger overhead.

Rendering and Parsing URLs

We’ve already mentioned how Yesod is able to automatically render type-safe URLs into textual URLs that can be inserted into an HTML page. Let’s say we have a route definition that looks like the following:

mkYesod "MyApp" [parseRoutes|
/some/path SomePathR GET
]

If we place SomePathR into a Hamlet template, how does Yesod render it? Yesod always tries to construct absolute ...

Get Developing Web Apps with Haskell and Yesod, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.