Hacker News new | past | comments | ask | show | jobs | submit login

I always wished SQL had a better handle on sum types. If I have a user whose favorite story is an instance of Either[Movie, Book], then it's already a pain to deal with in a nice simple way. And that's as simple as sum types get.



You can do that in EdgeDB:

  type Movie { 
    property director -> str
  };
  type Book {
    property author -> str
  };

  type User {
      multi link favorites -> Movie | Book
  };

  SELECT User {
    favorites: {
        [IS Movie].director,
        [IS Book].author,
    }
  };


Can you tell me whether I'm understanding this correctly?

Would this query result in, e.g. [(director: Null, author: J.K Rowling), (director: Spielberg, Null), ...] or would it be [author: J.K. Rowling, director: Spielberg, ...] or just plain strings: [J.K. Rowling, Spielberg, ...]? I still don't totally get the model here.


If fetched as JSON: {"favories": [{"director": null, "author": "J.K Rowling"}, {"director": "Spielberg", "author": null}, ...]}

You can also query the actual object type by referring to the __type__ link:

  SELECT User {
    favorites: {
        __type__,
        [IS Movie].director,
        [IS Book].author,
    }
  };


Thanks for the clarification! Seems really cool :)


This struck me just yesterday. You're left with using nulls (which is a sin punishable by poor data quality) or multiple tables for each sum type. Annoying.


Tables should be lightweight. It's the cruft the language and implementation that makes you avoid them.


That is an acute observation and something I might consider dumping SQL for, or rather, I feel SQL could be upgraded to include




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: