Search code examples
scalashapelesslenses

Add or modify values in a shapeless HMap


Does anyone know how I might add or modify values in a shapeless HMap? The only functions I see on the HMap definition are:

  • get
  • + (which looks like it's creating a new map and adding the (k,v) tuple)
  • - (same as above)

My suspicion is that I will need to use lens?


Solution

  • shapeless.HMap is immutable. It's a wrapper (with type-level enhancements) of scala.collection.immutable.Map.

    + adds or modifies a pair (returning new HMap).

    Lenses also create a copy. Immutability/persistence is typical for FP.