How to merge records only if the keys exist in the type

Hello,

Is there a way to do something like the following:

let Defaults = { Val1 = "val1", Val2 = "val2", Bar = "bar" }

let MyType =
      { Type = { Foo : Text, Bar : Text }
      , default = { Foo = "foo" } // Defaults
      }

in  MyType

but with something different that the operator // so that it only merges the keys if the key is defined in the record type.So in this case, I would like only Bar to be merged from the Defaults record and all other keys to be discarded.

So the output I’m looking for is:

{ Type = { Bar : Text, Foo : Text }
, default = { Bar = "bar", Foo = "foo" }
}

The closest thing I can think of is that you can project out a subset of keys by the expected type using the following syntax:

someRecord.(ExpectedType)

… so for your example you would be able to do this:

let Defaults = { Val1 = "val1", Val2 = "val2", Bar = "bar" }

let `Type` = { Foo : Text, Bar : Text }

let MyType =
      { Type
      , default = ({ Foo = "foo" } // Defaults).(`Type`)
      }

in  MyType

However, that will not work in general, because the default field does not necessarily define all of the fields present within the corresponding Type. It only happens to work in this example because the default field defines defaults for all of the fields.

Other than that, I’m not aware of any other solution.