fumieval / extensible

Extensible records, variants, structs, effects, tangles
BSD 3-Clause "New" or "Revised" License
128 stars 20 forks source link

Add coinclusionAssoc, wrenchAssoc, retrenchAssoc #36

Closed At-sushi closed 9 months ago

At-sushi commented 9 months ago

Features

Implemented for the convenience. These can be used to defaulting record values.

Example use-case

{-# LANGUAGE DataKinds, TypeOperators, FlexibleContexts, OverloadedLabels, OverloadedStrings, PolyKinds #-}

import Data.Extensible

type Parameters = Record '[
    "name" >: String
    , "width" >: Int
    , "height" >: Int
    , "id" >: Int
    ]

defaultPara :: Parameters
defaultPara =
    #name @= "untitled"
    <: #width @= 640
    <: #height @= 480
    <: #id @= -1
    <: nil

main :: IO ()
main = do
    let para = #id @= 0 <: #name @= "testApp" <: nil
        result = setDefaultValues defaultPara para
    print result -- name @= "testApp" <: width @= 640 <: height @= 480 <: id @= 0 <: nil

setDefaultValues :: (Generate ys, IncludeAssoc ys xs) => (ys :& h) -> (xs :& h) -> (ys :& h)
setDefaultValues ys = hzipWith fromNullable ys . wrenchAssoc