imports

{-# LANGUAGE UndecidableInstances #-}

module Plutarch.Docs.DerivingGenerics (PMyType (..)) where

import Plutarch.Prelude
import Data.Kind (Type)
import GHC.Generics (Generic)
import Generics.SOP qualified as SOP

Deriving typeclasses with generics

Plutarch also provides sophisticated generic deriving support for completely custom types. In particular, you can easily derive PlutusType for your own type:

data PMyType (a :: S -> Type) (b :: S -> Type) (s :: S)
  = POne (Term s a)
  | PTwo (Term s b)
  deriving stock (Generic)
  deriving anyclass (SOP.Generic)
  deriving (PlutusType) via (DeriveAsSOPStruct (PMyType a b))

Note: This requires the generics-sop package.

This will use a SOP encoding representation for MyType, which is typically what you want. If you want to use data encoding representation instead in your PlutusType instance - you should derive it using PlutusTypeData. Check out: implementing PIsDataRepr and friends

Currently, generic deriving supports the following typeclasses: