SHAPELESS MATRIX
ABOUT ME
Scala dev @
VirtusLab
Docker fan
PYTANIA
Scala?
Shapeless?
Typy i kompilator
Scali?
AGENDA
O Shapeless
Shapeless usages
Lenses
scala.Dynamic
HLists
Polymorphic functions
Generic[T]
Type level testing
OR Type
Podsumowanie
CO TO SHAPELESS?
UŻYCIA SHAPELESS
Breeze
Scodec
Specs
Anorm
Spray
LENSES
HLISTS
POLYMORPHIC FUNCTIONS
MOTYWACJA
// Foo.scala
class Foo {
def foo[T](a: T): Set[T] = Set(a)
val list = List(1,2,3)
list.map(foo)
}
KOMPILATOR
scalac -Xshow-phases
phase name id description
---------- -- -----------
parser 1 parse source into ASTs, perform simple desugaring
namer 2 resolve names, attach symbols to named trees
packageobjects 3 load package objects
typer 4 the meat and potatoes: type the trees
patmat 5 translate match expressions
superaccessors 6 add super accessors in traits and nested classes
extmethods 7 add extension methods for inline classes
pickler 8 serialize symbol tables
refchecks 9 reference/override checking, translate nested objects
uncurry 10 uncurry, translate function values to anonymous classes
tailcalls 11 replace tail calls by jumps
specialize 12 @specialized-driven class and method specialization
explicitouter 13 this refs to outer pointers
erasure 14 erase types, add interfaces for traits
posterasure 15 clean up erased inline classes
lazyvals 16 allocate bitmaps, translate lazy vals into lazified defs
lambdalift 17 move nested functions to top level
constructors 18 move field definitions into constructors
flatten 19 eliminate inner classes
mixin 20 mixin composition
cleanup 21 platform-specific cleanups, generate reflective calls
delambdafy 22 remove lambdas
icode 23 generate portable intermediate code
BYTECODE
javap -c Foo.scala
39: new #62 // class Foo$$anonfun$1
42: dup
43: aload_0
44: invokespecial #65 // Method Foo$$anonfun$1."<init>":(LFoo;)V
47: getstatic #51 // Field scala/collection/immutable/List$.MODULE$:Lscala/collecti
50: invokevirtual #69 // Method scala/collection/immutable/List$.canBuildFrom:()Lscala/
53: invokevirtual #75 // Method scala/collection/immutable/List.map:(Lscala/Function1;L
56: pop
57: return
</init>
TYPER
scalac -Xprint:typer Foo.scala
[[syntax trees at end of typer]] // Foo.scala
package <empty> {
class Foo extends scala.AnyRef {
def <init>(): Foo = {
Foo.super.<init>();
()
};
def foo[T](a: T): Set[T] = scala.this.Predef.Set.apply[T](a);
private[this] val list: List[Int] = immutable.this.List.apply[Int](1, 2, 3);
<stable> <accessor> def list: List[Int] = Foo.this.list;
Foo.this.list.map[Set[Int], List[Set[Int]]]({
((a: Int) => Foo.this.foo[Int](a))
})(immutable.this.List.canBuildFrom[Set[Int]])
}
}
</accessor></stable></init></init></empty>
GENERIC[T]
case class User(name: String, surname: String, email: String)
val genericUser = Generic[User]
val userInstance = User("Jan", "Kowalski", "jan.kowalski@foo.pl")
val userRepr = genericUser.to(userInstance)
val anotherUser = genericUser.from("Stefan" :: "Stefanowski" :: "stefcik@buziaczek.pl" :: HNil)
typed[String :: String :: String :: HNil](userRepr)
anotherUser shouldBe User("Stefan", "Stefanowski", "stefcik@buziaczek.pl")
TYPE LEVEL TESTING
import shapeless._
import test._
case class User(name: String, surname: String, email: String)
typed[Int](1)
val user = User("Jan", "Kowalski", "jan.kowalski@foo.pl")
typed[User](user)
// typed[User](1) <- won't compile
TYP OR

Bartosz kowalik Shapeless Matrix

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
    MOTYWACJA // Foo.scala class Foo{ def foo[T](a: T): Set[T] = Set(a) val list = List(1,2,3) list.map(foo) }
  • 11.
    KOMPILATOR scalac -Xshow-phases phase nameid description ---------- -- ----------- parser 1 parse source into ASTs, perform simple desugaring namer 2 resolve names, attach symbols to named trees packageobjects 3 load package objects typer 4 the meat and potatoes: type the trees patmat 5 translate match expressions superaccessors 6 add super accessors in traits and nested classes extmethods 7 add extension methods for inline classes pickler 8 serialize symbol tables refchecks 9 reference/override checking, translate nested objects uncurry 10 uncurry, translate function values to anonymous classes tailcalls 11 replace tail calls by jumps specialize 12 @specialized-driven class and method specialization explicitouter 13 this refs to outer pointers erasure 14 erase types, add interfaces for traits posterasure 15 clean up erased inline classes lazyvals 16 allocate bitmaps, translate lazy vals into lazified defs lambdalift 17 move nested functions to top level constructors 18 move field definitions into constructors flatten 19 eliminate inner classes mixin 20 mixin composition cleanup 21 platform-specific cleanups, generate reflective calls delambdafy 22 remove lambdas icode 23 generate portable intermediate code
  • 13.
    BYTECODE javap -c Foo.scala 39:new #62 // class Foo$$anonfun$1 42: dup 43: aload_0 44: invokespecial #65 // Method Foo$$anonfun$1."<init>":(LFoo;)V 47: getstatic #51 // Field scala/collection/immutable/List$.MODULE$:Lscala/collecti 50: invokevirtual #69 // Method scala/collection/immutable/List$.canBuildFrom:()Lscala/ 53: invokevirtual #75 // Method scala/collection/immutable/List.map:(Lscala/Function1;L 56: pop 57: return </init>
  • 14.
    TYPER scalac -Xprint:typer Foo.scala [[syntaxtrees at end of typer]] // Foo.scala package <empty> { class Foo extends scala.AnyRef { def <init>(): Foo = { Foo.super.<init>(); () }; def foo[T](a: T): Set[T] = scala.this.Predef.Set.apply[T](a); private[this] val list: List[Int] = immutable.this.List.apply[Int](1, 2, 3); <stable> <accessor> def list: List[Int] = Foo.this.list; Foo.this.list.map[Set[Int], List[Set[Int]]]({ ((a: Int) => Foo.this.foo[Int](a)) })(immutable.this.List.canBuildFrom[Set[Int]]) } } </accessor></stable></init></init></empty>
  • 15.
    GENERIC[T] case class User(name:String, surname: String, email: String) val genericUser = Generic[User] val userInstance = User("Jan", "Kowalski", "jan.kowalski@foo.pl") val userRepr = genericUser.to(userInstance) val anotherUser = genericUser.from("Stefan" :: "Stefanowski" :: "stefcik@buziaczek.pl" :: HNil) typed[String :: String :: String :: HNil](userRepr) anotherUser shouldBe User("Stefan", "Stefanowski", "stefcik@buziaczek.pl")
  • 16.
    TYPE LEVEL TESTING import shapeless._ import test._ caseclass User(name: String, surname: String, email: String) typed[Int](1) val user = User("Jan", "Kowalski", "jan.kowalski@foo.pl") typed[User](user) // typed[User](1) <- won't compile
  • 17.