Artwork

Content provided by Dick Wall and Scalawags Podcast. All podcast content including episodes, graphics, and podcast descriptions are uploaded and provided directly by Dick Wall and Scalawags Podcast or their podcast platform partner. If you believe someone is using your copyrighted work without your permission, you can follow the process outlined here https://player.fm/legal.
Player FM - Podcast App
Go offline with the Player FM app!

Scalawags #17 - Shapeless

1:38:06
 
Share
 

Archived series ("Inactive feed" status)

When? This feed was archived on March 01, 2020 12:09 (4y ago). Last successful fetch was on August 13, 2019 01:12 (4+ y ago)

Why? Inactive feed status. Our servers were unable to retrieve a valid podcast feed for a sustained period.

What now? You might be able to find a more up-to-date version using the search function. This series will no longer be checked for updates. If you believe this to be in error, please check if the publisher's feed link below is valid and contact support to request the feed be restored or if you have any other concerns about this.

Manage episode 39979117 series 11533
Content provided by Dick Wall and Scalawags Podcast. All podcast content including episodes, graphics, and podcast descriptions are uploaded and provided directly by Dick Wall and Scalawags Podcast or their podcast platform partner. If you believe someone is using your copyrighted work without your permission, you can follow the process outlined here https://player.fm/legal.

our guest today is Miles Sabin, author of Shapeless

Intro (0:00)
  • Shapeless has a theme tune, provided by Ed Kmett and featuring Bruce Lee
  • Sabin is pronounced “Sabin”
  • Josh catches and eats raccoons, as does nearly everyone in western Pennsylvania
What is Shapeless? (5:00)
  • Shapeless is just HLists, right?
  • Shapeless is just a “showoff project”, right?
  • Miles’ dog is heard from and briefly seen
    • Or is it a guest dog?
  • Miles is shapeless, Dick is shameless
What is an HList? (9:50)
  • HList crosses tuples with lists:
    • H is for heterogeneous
    • Variable length like list
    • Each element has its own compile-time type, like a tuple
  • The simplest possible HList implementation is just 6 lines of Scala
  • They're useful for abstracting over arity
    • e.g. of tuples, of case classes, of functions
Generic programming (15:00)
  • Shapeless began with Miles trying to do generic programming, a la Haskell’s Scrap Your Boilerplate, in Scala
  • Generic programming is “the kind of polymorphism your language doesn’t support yet”?
  • Generic programming is “second-class polymorphism”?
  • Generic programming abstracts over the “shape” of diverse data types
  • Arity is an example; so is depth of nesting
  • Generic tree transformation, generic folds on trees, serialization and deserialization. XML, JSON.
  • The best of both worlds: avoiding boilerplate while staying type-safe
  • Michael Pilquist's scodec library uses Shapeless to do binary serialization and deserialization
HLists again (26:05) Generic programming again (30:00)
  • again, HList lets you abstract over arity
  • example: composing validations
    • Scalaz internally has boilerplate code for this for a fixed set of arities only
  • Miles disclaims any desire to reinvent Scalaz
KList and AList (34:15)
  • KList requires the element types to share an outer type constructor
  • AList allows multiple type constructors
  • Daniel says KList and AList are unsound in Scala because of variance
    • there’s an “unchecked-variance loophole”
  • Shapeless doesn't have AList and doesn't need KList
    • in Shapeless a KList is just an HList with a constraint
    • the constraint is maintained by requiring an implicit witness
  • Shapeless uses Poly for mapping functions over HLists
  • Daniel says Lars Hupel says there are problems combining Poly with Scalaz’s Unapply?
    • Miles is skeptical, asks for gist
  • Josh wonders if sbt ought to unify HList, KList, and AList too
  • what's the trick for unifying them? Poly!
Polymorphic functions (43:10) Algebraic data types (47:00)
  • But we don't just care about HLists
    • HList is a particular data “shape”
    • Shapeless aims to be generic over all shapes
  • “Algebraic” data types
    • Sum types (e.g. Either, sealed traits) and product types (e.g. tuples, case classes)
    • Complex data types can be understood (and processed generically) as recursively nested sums and products
  • Coproducts
  • We're all writing boilerplate code for folds and transforms all the time
  • If we could do it generically, our code would be smaller
Performance, compile-time and runtime (56:00)
  • Would our smaller code also be slower?
    • Expressiveness vs. performance?
  • Daniel: Java 8 JVM helps
  • Dick: can you have a million-element HList?
  • Dale somebody asks: compile-time performance penalty, runtime performance penalty, or both?
  • Shapeless tests often test whether something compiles, rather than what the answer is
  • discussion on what factors affect Scala compile times
    • Daniel: don't scapegoat cake pattern
    • Josh: if your compile times are too long, don't obsess over which language features or patterns compile slowly; concentrate on architecture and controlling dependencies
Labels and lenses (1:05:05)
  • Dick wants to hear about lenses
  • Miles needs to explain Generic (formerly Iso) first
  • Generic is a small macro, one of only a few in Shapeless
  • Generic maps back and forth between case class types and HList types
  • “Labels”, brand new in recent months
    • Adding labels to Generic gives LabeledGeneric
    • In a sum type (family of case classes), the labels are the class names
    • In a product type (particular case class), the labels are the field names
    • You need the names to do serialiation and deserialization
  • New Shapeless lenses built on top of LabeledGeneric
    • Try them in Shapeless 2.1-SNAPSHOT
  • Scala's SelectDynamic is used also
  • Miles also recommends looking at Monocle
  • Miles would like to take Shapeless lenses in the same direction
    • But doing so would involve depending on Scalaz
    • Scalaz good, more dependencies bad. Dilemma
    • Shapeless lenses may become a separate project, with the Scalaz dependency
  • (we apologize for all the beeping in this section and the next)
Listener Q&A (1:17:05)
  • (we apologize for losing the askers’ names)
  • “What happened to unboxed tagged types?”
    • Miles: See migration guide. They've moved.
    • Tagged types are like newtype in Haskell
    • Labels are implemented using tagged types, with the singleton types of strings
  • “What’s the Haskell equivalent to Shapeless?”
  • “Is anything in Shapeless flaky and I should wait before using it?”
    • Miles: the way you define Polys inline is likely to change.
    • But relax because if anything goes wrong it will go wrong at compile time.
  • “What is the Either-like structure you mentioned that we get from a sealed trait?”
    • Miles: That’s the coproduct. It only works if what you’re doing is just like an ADT in Haskell.
    • Obligatory animal example
    • HLists are nested pairs, coproducts are nested Eithers
    • Daniel: It's Lisp at the type level.
  • SHAPELESS IS ABOUT GENERIC PROGRAMMING NOT HLISTS
  • Miles enjoys doing long rambly Shapeless workshops
  • Miles will speak at Scala Days in Berlin, with Jon Pretty, about 10 years of doing Scala
  • Josh fights Bruce Lee and guess what: Bruce wins
  continue reading

41 episodes

Artwork

Scalawags #17 - Shapeless

thescalawags's podcast

78 subscribers

published

iconShare
 

Archived series ("Inactive feed" status)

When? This feed was archived on March 01, 2020 12:09 (4y ago). Last successful fetch was on August 13, 2019 01:12 (4+ y ago)

Why? Inactive feed status. Our servers were unable to retrieve a valid podcast feed for a sustained period.

What now? You might be able to find a more up-to-date version using the search function. This series will no longer be checked for updates. If you believe this to be in error, please check if the publisher's feed link below is valid and contact support to request the feed be restored or if you have any other concerns about this.

Manage episode 39979117 series 11533
Content provided by Dick Wall and Scalawags Podcast. All podcast content including episodes, graphics, and podcast descriptions are uploaded and provided directly by Dick Wall and Scalawags Podcast or their podcast platform partner. If you believe someone is using your copyrighted work without your permission, you can follow the process outlined here https://player.fm/legal.

our guest today is Miles Sabin, author of Shapeless

Intro (0:00)
  • Shapeless has a theme tune, provided by Ed Kmett and featuring Bruce Lee
  • Sabin is pronounced “Sabin”
  • Josh catches and eats raccoons, as does nearly everyone in western Pennsylvania
What is Shapeless? (5:00)
  • Shapeless is just HLists, right?
  • Shapeless is just a “showoff project”, right?
  • Miles’ dog is heard from and briefly seen
    • Or is it a guest dog?
  • Miles is shapeless, Dick is shameless
What is an HList? (9:50)
  • HList crosses tuples with lists:
    • H is for heterogeneous
    • Variable length like list
    • Each element has its own compile-time type, like a tuple
  • The simplest possible HList implementation is just 6 lines of Scala
  • They're useful for abstracting over arity
    • e.g. of tuples, of case classes, of functions
Generic programming (15:00)
  • Shapeless began with Miles trying to do generic programming, a la Haskell’s Scrap Your Boilerplate, in Scala
  • Generic programming is “the kind of polymorphism your language doesn’t support yet”?
  • Generic programming is “second-class polymorphism”?
  • Generic programming abstracts over the “shape” of diverse data types
  • Arity is an example; so is depth of nesting
  • Generic tree transformation, generic folds on trees, serialization and deserialization. XML, JSON.
  • The best of both worlds: avoiding boilerplate while staying type-safe
  • Michael Pilquist's scodec library uses Shapeless to do binary serialization and deserialization
HLists again (26:05) Generic programming again (30:00)
  • again, HList lets you abstract over arity
  • example: composing validations
    • Scalaz internally has boilerplate code for this for a fixed set of arities only
  • Miles disclaims any desire to reinvent Scalaz
KList and AList (34:15)
  • KList requires the element types to share an outer type constructor
  • AList allows multiple type constructors
  • Daniel says KList and AList are unsound in Scala because of variance
    • there’s an “unchecked-variance loophole”
  • Shapeless doesn't have AList and doesn't need KList
    • in Shapeless a KList is just an HList with a constraint
    • the constraint is maintained by requiring an implicit witness
  • Shapeless uses Poly for mapping functions over HLists
  • Daniel says Lars Hupel says there are problems combining Poly with Scalaz’s Unapply?
    • Miles is skeptical, asks for gist
  • Josh wonders if sbt ought to unify HList, KList, and AList too
  • what's the trick for unifying them? Poly!
Polymorphic functions (43:10) Algebraic data types (47:00)
  • But we don't just care about HLists
    • HList is a particular data “shape”
    • Shapeless aims to be generic over all shapes
  • “Algebraic” data types
    • Sum types (e.g. Either, sealed traits) and product types (e.g. tuples, case classes)
    • Complex data types can be understood (and processed generically) as recursively nested sums and products
  • Coproducts
  • We're all writing boilerplate code for folds and transforms all the time
  • If we could do it generically, our code would be smaller
Performance, compile-time and runtime (56:00)
  • Would our smaller code also be slower?
    • Expressiveness vs. performance?
  • Daniel: Java 8 JVM helps
  • Dick: can you have a million-element HList?
  • Dale somebody asks: compile-time performance penalty, runtime performance penalty, or both?
  • Shapeless tests often test whether something compiles, rather than what the answer is
  • discussion on what factors affect Scala compile times
    • Daniel: don't scapegoat cake pattern
    • Josh: if your compile times are too long, don't obsess over which language features or patterns compile slowly; concentrate on architecture and controlling dependencies
Labels and lenses (1:05:05)
  • Dick wants to hear about lenses
  • Miles needs to explain Generic (formerly Iso) first
  • Generic is a small macro, one of only a few in Shapeless
  • Generic maps back and forth between case class types and HList types
  • “Labels”, brand new in recent months
    • Adding labels to Generic gives LabeledGeneric
    • In a sum type (family of case classes), the labels are the class names
    • In a product type (particular case class), the labels are the field names
    • You need the names to do serialiation and deserialization
  • New Shapeless lenses built on top of LabeledGeneric
    • Try them in Shapeless 2.1-SNAPSHOT
  • Scala's SelectDynamic is used also
  • Miles also recommends looking at Monocle
  • Miles would like to take Shapeless lenses in the same direction
    • But doing so would involve depending on Scalaz
    • Scalaz good, more dependencies bad. Dilemma
    • Shapeless lenses may become a separate project, with the Scalaz dependency
  • (we apologize for all the beeping in this section and the next)
Listener Q&A (1:17:05)
  • (we apologize for losing the askers’ names)
  • “What happened to unboxed tagged types?”
    • Miles: See migration guide. They've moved.
    • Tagged types are like newtype in Haskell
    • Labels are implemented using tagged types, with the singleton types of strings
  • “What’s the Haskell equivalent to Shapeless?”
  • “Is anything in Shapeless flaky and I should wait before using it?”
    • Miles: the way you define Polys inline is likely to change.
    • But relax because if anything goes wrong it will go wrong at compile time.
  • “What is the Either-like structure you mentioned that we get from a sealed trait?”
    • Miles: That’s the coproduct. It only works if what you’re doing is just like an ADT in Haskell.
    • Obligatory animal example
    • HLists are nested pairs, coproducts are nested Eithers
    • Daniel: It's Lisp at the type level.
  • SHAPELESS IS ABOUT GENERIC PROGRAMMING NOT HLISTS
  • Miles enjoys doing long rambly Shapeless workshops
  • Miles will speak at Scala Days in Berlin, with Jon Pretty, about 10 years of doing Scala
  • Josh fights Bruce Lee and guess what: Bruce wins
  continue reading

41 episodes

All episodes

×
 
Loading …

Welcome to Player FM!

Player FM is scanning the web for high-quality podcasts for you to enjoy right now. It's the best podcast app and works on Android, iPhone, and the web. Signup to sync subscriptions across devices.

 

Quick Reference Guide