Scala Days 2013 Notes from Expanding eta-expansion: shapeless polymorphic function values meet macros

What could be better to master Scala and presentation skills than to watch people who do them better? Since the recordings of the presentations from Scala Days 2013 are available on Parleys at no charge I thought I’d spend some time and watch a few.

I did really watch a few, but just recently I decided to review them to better understand the topics and perhaps absorb their wisdom in a more effective way.

Quite recently I was watching the recording from the session Expanding eta-expansion: shapeless polymorphic function values meet macros by Miles Sabin. It was a quite informative presentation, but to be honest just the first part before Miles switched to the vim editor and explained the shapeless project was the most valuable. I seemed to not have been able to follow along later (when he talked about shapeless and its purpose). The speaking style was very engaging and despite my lack of understanding of the 2nd part I could listen to the end with ease.

Here are some sentences from the presentation that I really liked and a few comments about it (the presentation). The excerpts comprise the time and what Miles said (so trying to write the manuscript I had to rewind a few times and eventually remembered the most).

00:01:37 What is eta expansion?
“eta expansion is a mechanism whereby Scala tries to gloss over the distinction between methods which are intrinsic components of JVM that are a sort of artifacts which are attached to classes and interfaces – the lowest level native representation of types on the JVM. Functions, on the other hand, are Scala-specific artifacts. They’re represented as first-class values and eta expansion is a mechanism which allows to in many circumstances completely seemlessly slide (?) between the two.”

00:02:30 “Show very quickly what that actually means in practice.”

I was wondering why Miles didn’t use the following code to populate a list of lists. I also think it’d have been better if the code had been already available (in a git tag perhaps) so there had not been necessary to type it in.

scala> (1 to 3).map(List(_)).toList
res5: List[List[Int]] = List(List(1), List(2), List(3))

00:06:48 “Scala does a very good syntactic job of hiding a transformation that’s been formed on the arguments of the map and done in a such seamless way that it makes it appear that the method headOpt[T] is in fact an entity of the same category as a function value of either List[Int] or List[String] to Option[Int] or Option[String], but in fact it’s not.”

It’d be definitely much better when he’d used :paste and tab key in REPL.

00:07:48 “If headOpt is a function, it should be perfectly possible to redefine headOptInt in terms of a function type.”

00:08:47 “The thing is that we actually have now something which is…although the method we started with is monomorphic the value that we have here is…”

00:09:08 “Before I go anywhere further I want to illustrate that the original headOptString is a value and it’s a value in the JVM’s sense of being an object that will have for example hashCode whereas headOpt is not a first-class value and doesn’t have hashCode.”

00:09:41 “It [headOpt] is clearly not a kind of thing which we can treat as being a Java value.”

00:09:58 “So what happens under the hood [with headOptInt assignment] is that seamlessly the Scala compiler had inserted this magic trailing underscore. And the magic trailing underscore is something which allows us to explicitly ask for a method value to be lifted to a function value. What actually happened is that under the hood the Scala compiler has manufactured a brand new value of Function1 type with its argument type instantiated to a list of Int result type instantiated to Option[Int].”

00:14:05 “Any is almost as useless as Nothing in this case.”

00:16:35 “In this case we do have something that’s clearly a value => headOpt returns headOpt.type.”

00:18:00 The speaker should’ve used map over hl to convert hl to a list of lists and then done the rest. But :paste would’ve been even better.

00:28:17 “I dearly would like to write that…just directly use a polymorphic and oridinary vanilla Scala polymorphic method and have that worked directly. Oh, we can. It worked! How is that possible? smiling…”

00:29:35 “Perhaps an unsurprising trick involved here is an implicit conversion from a function type to a polymorphic function value type. And a sneaky and evil trick involved here is to actually in effect consume this rather useless function from a List[Nothing] to Option[Nothing]. Inspect it at compile time var macro, extract out the useful part which I guess we will see very shortly…”

Then Miles went on to demonstrating code of Shapeless that I didn’t find much useful to understand eta-expansion. Although I watched till the very end, I found no information I meant to put here to remember. It was worth to watch the 2nd part, anyway.

Be Sociable, Share!
This entry was posted in Languages.

Leave a Reply

%d bloggers like this: