Haskell Cookbook
Practical, copy-paste-ready recipes for Haskell code generation. For the full API of each spec type, see Building Functions & Fields, Building Types & Enums, and Files & Projects.
Data record with deriving
use sigil_stitch::prelude::*;
let type_spec = TypeSpec::builder("Person", TypeKind::Struct)
.add_field(
FieldSpec::builder("personName", TypeName::primitive("String")).build().unwrap(),
)
.add_field(
FieldSpec::builder("personAge", TypeName::primitive("Int")).build().unwrap(),
)
.implements(TypeName::primitive("Show"))
.implements(TypeName::primitive("Eq"))
.build()
.unwrap();
data Person =
Person {
personName :: String,
personAge :: Int,
}
deriving (Show, Eq)
Type class
use sigil_stitch::prelude::*;
let type_spec = TypeSpec::builder("Printable", TypeKind::Trait)
.doc("Things that can be printed.")
.add_method(
FunSpec::builder("prettyPrint")
.add_param(ParameterSpec::new("a", TypeName::primitive("a")).unwrap())
.returns(TypeName::primitive("String"))
.build()
.unwrap(),
)
.build()
.unwrap();
-- | Things that can be printed.
class Printable where
prettyPrint :: a -> String
Function with split signature
use sigil_stitch::prelude::*;
let body = CodeBlock::of("x + y", ()).unwrap();
let fun = FunSpec::builder("add")
.add_param(ParameterSpec::new("x", TypeName::primitive("Int")).unwrap())
.add_param(ParameterSpec::new("y", TypeName::primitive("Int")).unwrap())
.returns(TypeName::primitive("Int"))
.body(body)
.build()
.unwrap();
add :: Int -> Int -> Int
add x y =
x + y
Newtype
use sigil_stitch::prelude::*;
let type_spec = TypeSpec::builder("Meters", TypeKind::Newtype)
.extends(TypeName::primitive("Int"))
.build()
.unwrap();
newtype Meters = Meters Int
Type alias
use sigil_stitch::prelude::*;
let type_spec = TypeSpec::builder("Name", TypeKind::TypeAlias)
.extends(TypeName::primitive("String"))
.build()
.unwrap();
type Name = String