Justin du Coeur (jducoeur) wrote,
Justin du Coeur

Mad Science with Types

[For the programmers]

Continuing yesterday's discussion of the shapeless library -- programmers who want to have their minds blown (or at least expanded) should take a look at the overview of shapeless 2.0. It goes *way* beyond the parts I mentioned yesterday, with concepts including:
  • Truly polymorphic functions: a way to build functions that operate cleanly on multiple arbitrary Types. (Which is normally impossible in Scala.)

  • The ability to use all of the usual List-like operations (head, tail, concatenation, etc) on tuples.

  • HMap: a variant of Map that allows you to have keys of multiple types.

  • Singleton Types: that is, "the Type that is the Integer 23", "the Type that is the string 'foo'", and so on.

  • Extensible Records: sort of a hybrid between a compile-time class and a runtime Map -- heterogeneous and extensible, yet strongly-typed.

  • Discriminated Unions (yay! One of the most-asked-for features in Scala).

  • Lenses: strongly-typed "views" of a subset of the fields of a Type (including arbitrary fields of nested Types inside classes).

  • Literal collections whose sizes are encoded in the type system, so that you can check that they match at compile time.
To me, the thing about all of this isn't just the library itself: it's that it is doing so many things that I wouldn't have even considered *possible* before. These functions do all sorts of things we do all the time -- but usually at the expense of breaking type-safety. Basically, shapeless is a whole different way of looking at the world, showing that in many cases you *can* have your cake and eat it too: if you're sufficiently clever, Scala allows you to do remarkable things with completely strong typing, and shapeless makes the process of doing so reasonable for real code.

(Mind, I gather all of this can be done in Haskell as well -- it's just remarkable to see it in a language I actually use. It's a fine demonstration that, when used really well, Scala can be far more strongly-typed than something like Java or C#, for equivalent problems.)

Of course, the downside is that I'm starting to realize lots of places where I should be using this stuff in Querki. Adopting shapeless properly is going to be a long-term project...
Tags: programming, scala

  • Adtech

    Here's an interesting article about "adtech" -- those automated algorithms that companies like Google and Facebook use to spy on you and serve up…

  • Chrome instability?

    For the past week or two, Chrome has become surprisingly unstable -- it's been crashing on me about once a day. Weirdly, it is usually when I'm not…

  • Crossing the Uncanny Valley

    [Trying out posting from DreamWidth. Let's see if everything is configured right.] Just saw Rogue One. Capsule Summary: not an epic for the ages,…

  • Post a new comment


    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded