flix / flix

The Flix Programming Language
https://flix.dev/
Other
2.08k stars 150 forks source link

NamedAst: introduce `NestedMap` or `NamespaceMap` or some such structure #3726

Closed mlutze closed 1 year ago

mlutze commented 2 years ago

In the Namer we have a big block where many times we have to access and modify a map within a map, leading to code that looks like

                    prog0.copy(
                      classes = prog0.classes + (ns0 -> (classes0 + (ident.name -> clazz))),
                      defsAndSigs = prog0.defsAndSigs + (sigNs -> defsAndSigs))

It's difficult to read and easy to make a mistake. I'd like to introduce some new structure that makes the nested map easier to manipulate.

magnus-madsen commented 2 years ago

Lenses? Half-kidding.

If you propose an API we could implement it in both Flix and Scala.

mlutze commented 2 years ago

Something like this?

enum DeepMap[k1, k2, v](Map[k1, Map[k2, v]])

def putDeep(k1: k1, k2: k2, v: v, m: DeepMap[k1, k2, v]) =
  let DeepMap(outer) = m;
  let inner = outer |> Map.getOrDefault(k1);
  let newInner = inner |> Map.put(k2, v);
  let newOuter = outer |> Map.put(k1, newInnder);
  DeepMap(newOuter)

I think it's too specialized to add to the standard library.

magnus-madsen commented 2 years ago

Seems too specialized.

mlutze commented 1 year ago

Closing since Magnus doesn't like it :P