A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/exts/derive_any_class.html below:

Website Navigation


6.6.6. Deriving any other class — Glasgow Haskell Compiler 9.12.2 User's Guide

6.6.6. Deriving any other class¶
DeriveAnyClass¶
Since:

7.10.1

Allow use of any typeclass in deriving clauses.

With DeriveAnyClass you can derive any other class. The compiler will simply generate an instance declaration with no explicitly-defined methods. This is mostly useful in classes whose minimal set is empty, and especially when writing generic functions.

As an example, consider a simple pretty-printer class SPretty, which outputs pretty strings:

{-# LANGUAGE DefaultSignatures, DeriveAnyClass #-}

class SPretty a where
  sPpr :: a -> String
  default sPpr :: Show a => a -> String
  sPpr = show

If a user does not provide a manual implementation for sPpr, then it will default to show. Now we can leverage the DeriveAnyClass extension to easily implement a SPretty instance for a new data type:

data Foo = Foo deriving (Show, SPretty)

The above code is equivalent to:

data Foo = Foo deriving Show
instance SPretty Foo

That is, an SPretty Foo instance will be created with empty implementations for all methods. Since we are using DefaultSignatures in this example, a default implementation of sPpr is filled in automatically.

Note the following details


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4