Skip to content

overwriting namespace of purescript-record #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 0 additions & 36 deletions src/Data/Record.purs

This file was deleted.

File renamed without changes.
35 changes: 34 additions & 1 deletion src/Data/Record/Class.purs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
module Data.Record.Class (
class Subrow,
class RecordMerge,
class IntersectRow
class IntersectRow,
merge,
unionMerge
) where

-- | Proof that row `r` is a subset of row `s`
Expand All @@ -16,3 +18,34 @@ instance irInst :: (Union r i ri, Union i s si) => IntersectRow ri si i r s

class RecordMerge (o :: # Type) (mr :: # Type) (mo :: # Type)
instance rmInst :: (IntersectRow mo mr m o r, Subrow r o) => RecordMerge o mr mo


-- | Merge any two records together unsafely.
-- | Fields common between the two will result in the value from r2 being kept
foreign import unsafeMerge
:: forall r1 r2 r3
. Record r1
-> Record r2
-> Record r3

-- | Merge a record `mr` with optional default values `o`, resulting in record `mo`.
-- |
-- | The record `mr` must consist of the common fields from `mo` and `mr` plus a subset
-- | of fields from `o`.
-- |
-- | Examples:
-- | * `merge {a:1,b:"Unspecified"} {a:3,c:"Mandatory"} = {a:3,b:"Unspecified",c:"Mandatory"}`
-- | * `merge {a:1,b:"Unspecified"} {c:"Only mandatory"} = {a:1,b:"Unspecified",c:"Only Mandatory"}`
-- | * `merge {a:1,b:"Unspecified"} {a:"Wrong type"} = wont compile`
merge :: forall o mr mo. RecordMerge o mr mo => Record o -> Record mr -> Record mo
merge = unsafeMerge

-- | Merge record `a` with `b` resulting in `c`. The `Union` constraint means that `c`
-- | will contain all the fields from both `a` and `b`, with `b`'s appearing first in the
-- | list of types.
-- |
-- | Examples:
-- | * `unionMerge {a:"Default"} {a:1} = {a:1} :: {a::Int,a::String}`
-- | * `unionMerge {a:"Default",b:2} {a:1} = {a:1,b:2} :: {a::Int,a::String,b::Int}`
unionMerge :: forall a b c. Union b a c => Record a -> Record b -> Record c
unionMerge = unsafeMerge