Loading presentation...

Present Remotely

Send the link below via email or IM

Copy

Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.

DeleteCancel

Make your likes visible on Facebook?

Connect your Facebook account to Prezi and let your likes appear on your timeline.
You can change this under Settings & Account at any time.

No, thanks

A Type-Safe Solar System

No description
by

Alois Cochard

on 11 March 2014

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of A Type-Safe Solar System

A Type-Safe Solar System
A gentle introduction to DSL design in Scala
http://github.com/timeoutdigital/talk-solarsystem
Challenges
SolrJ document interfaces dichotomy
Read API
Write API
Challenges
"Schema-less" but we still want safety at "fields level"
Challenges
Need a first working implementation ASAP
Using extensible records or row polymorphism is out of scope
Writing
First we define the operations available on a Field
Set - set a field value
Add - add a value to multi-valued field
An operation mutates a SolrInputDocument
Writing
Lazy!
Writing
Operations compose
Writing
We create a new document using `InputDocument`
Writing
But how do we know which types are supported by Solr?
Type Class!
Writing
Let's use the Type Class in operations
Why putting the `writer` implicit there?
Why not on the `apply` method, where it is actually needed?
Writing
Let's add some instances for types supported natively by Solr
Writing
"Well guys, that's just a Contrafunctor"
Daniele Turi
A Contra-WHAT????
Writing
Writing
All right, let's make our Writer a Contrafunctor then!
Reading
Same approach as for writing, just simpler
That horrible casting is necessary because Solr return `Any`
Reading
Some instances for types natively supported by Solr
Reading
Wrapping SolrJ document
Query
There is multiple ways were the schema can be beneficial for building queries
For example
Filtering
Sorting
Boost
...
q=pizza&fq=cuisines:italian&bq=price^20
Query
Query
Let's integrate the schema in our Query builder
Ideally, we would be nice to support collection like syntax...
Usage
Let's put everything together, we need a model first.
But what is that `SealedObjects` thing???
Usage
A simple schema definition
Ideally, the city writer should be derived automatically
Usage
Read / Write / Query
Safe!
Tradeoff
Using immutability and laziness can have bad impact on performance.
With little changes, we can make the DSL behave in a mutable way!

No more object allocation... but more coupling and less composable.
Tradeoff
No safety at "document level"
That is at the moment not an easy one to solve and is a subject of research ...
Research
Very naive syntax
How can we get the best of both world?
Extensible records
Row polymorphism
The syntax is a bit less naive,
but still far from anything doable in vanilla scala
Research
Further hacking
shapeless (scala library)
lot of crazy things!
including extensible records
https://github.com/milessabin/shapeless
type provider in macro paradise (scala)
http://docs.scala-lang.org/overviews/macros/typeproviders.html
Travis Brown schema binding (scala)
http://meta.plasm.us/posts/2013/06/19/macro-supported-dsls-for-schema-bindings/
ctrex (haskell library)
extensible records
http://www.haskell.org/haskellwiki/CTRex
vinyl (haskell library)
extensible records
https://hackage.haskell.org/package/vinyl
purescript (experimental programming language)
row polymorphism
http://purescript.readthedocs.org/en/latest/types.html#row-polymorphism
Thanks!
Questions ?
@aloiscochard
http://github.com/aloiscochard
Tips!
Steal it!
ADTDD ... a new fancy acronym
Introduction
Basic DSL design techniques
Using Solr client for Java
Wrapping it in a Scala DSL
Available scala libraries:
Slashem
ScalalikeSolr
Use object mapping or fully dynamic approach
Basically we went structural typing instead of nominative typing.
NOTE: The code is actually wrong!

The functions `person` and `address` should take `Address` and `Person`.
Original idea by Travis Brown:
http://stackoverflow.com/questions/13671734/iteration-over-a-sealed-trait-in-scala
Algebraic Data Type Driven Development
In fact it's very close to what you can do TODAY with Shapeless
s/Cofunctor/Contrafunctor/
Full transcript