tazjin/yants

n-ary either

Ptival opened this issue · 3 comments

I find the nesting of eithers annoying to write, and inconsequential. As a result, there should probably be a one-of sort of type constructor, s.t. oneof [a b c] either computes to either<a,either<b,c>>, or be its own type that behaves as such.

You might want to call it union, or you might want to avoid such language! :)

Do the sum types (which don't currently have a corresponding screenshot in the README :S) work for this?

let thing = sum "thing" {
  a = int;
  b = option string;
  c = struct { name = string; };
};
in thing { a = 42; }

This requires explicit tagging of (named) variants, it sounds like you want anonymous variants?

Sadly, I am typing values as they appear in nixpkgs, so I have no control over the values and must assign types a posteriori. So I don't get to tag them.

Equally sadly, fields as they appear in nix derivations have all sorts of quirks, with no guidance as to what they should be. This came up in particular as I was trying to assign a structured type to the urls field of a derivation's source. It turns out that urls, as they appear in the wild, can be, at least, any of:

  • a string
  • a list of strings
  • a list of lists of strings

So far those are the only use cases I have encountered (and since this is dynamic typing, I only get to know about other cases when I end up evaluating them!).

So yeah, discriminated unions are great, and people should use them, but sometimes life sucks and you have untagged unions. :-(

Okay, this makes sense - I might not get around to adding this in the next couple of days though, it's going to be a busy week over here. Feel free to send a pull request for it (should be straightforward to implement), otherwise I'll do it once I get the chance!