query
On this page

mergeTypes

lib.types.mergeTypes

Docs pulled from | This Revision | 10 minutes ago


Merges two option types together.

Uses the type merge function of the first type, to merge it with the second type.

Usually types can only be merged if they are of the same type

Inputs

: a (option type): The first option type.
b (option type): The second option type.

Returns

  • The merged option type.
  • { _type = "merge-error"; error = "Cannot merge types"; } if the types can't be merged.

Examples

lib.types.mergeTypes usage example

let
  enumAB = lib.types.enum ["A" "B"];
  enumXY = lib.types.enum ["X" "Y"];
  # This operation could be notated as: [ A ] | [ B ] -> [ A B ]
  merged = lib.types.mergeTypes enumAB enumXY; # -> enum [ "A" "B" "X" "Y" ]
in
  assert merged.check "A"; # true
  assert merged.check "B"; # true
  assert merged.check "X"; # true
  assert merged.check "Y"; # true
  merged.check "C" # false

Noogle detected

Implementation

The following is the current implementation of this function.

mergeTypes =
      a: b:
      assert isOptionType a && isOptionType b;
      let
        merged = a.typeMerge b.functor;
      in
      if merged == null then setType "merge-error" { error = "Cannot merge types"; } else merged;