0
Internal DSLs
   in Scala
     Zef Hemel
   Medewerker no. 0855447
Java++
header("Add entry")
form {
  table {
    row {
       col { text("Your name:") }
       col { newEntry.name = input(newEnt...
in Scala
Pages
case class Home() extends Page {
  def ui {
    header("Welcome to my guestbook!")
    section {
      header("All entries...
Templates
case class Home() extends Page {
  def ui {
    header("Welcome to my guestbook!")
    section {
      entries
    }
  }

...
abstract class MainTemplate extends Page {
  def body

    def ui {
      block("headerblock") {
         header {
       ...
case class Home() extends MainTemplate {
  def body {
    header("Welcome to my guestbook!")
    section {
      entries
 ...
Styling
object DefaultStyle extends Style {
  block("headerblock") >> header {
    fontsize = 30 pt;
    width = 100 percent;
    ...
abstract class MainTemplate extends Page {
  def body

    style = DefaultStyle

    def ui {
      block("headerblock") {...
Access Control
(added this morning in 15 lines of code)
case class CannotEnter() extends Page with AccessControl {
  def canAccess = false

    def ui {
      text("You should ne...
Data model
class Entry extends DataObject {
  def singleton = Entry

    @Column var name : String = ""
    @Column var text : String...
How?
OO features

basic: classes, objects, inheritance, reflection


                    traits
function
programming
   closures
flexible syntax

         a >> b == a.>>(b)

form { ... } ~= form(() => { ... })
implicits

8 pt == new UnitIntWrapper(8).pt

Entry.all == Entry.all(dbSession)
type inference

    var newEntry = Entry()
              ==
var newEntry : Entry = Entry()
Advantages
“languages” as libraries


trivial “language” composition
Limitations
Scala → JVM
model checking
      ==
type checking
boiler plate

case class Home() extends Page {
  def ui {
  }
}
Upcoming SlideShare
Loading in...5
×

Internal DSLs Scala

1,314

Published on

My brief talk about my attempt to build an internal version of the WebDSL language.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,314
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
26
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Internal DSLs Scala"

  1. 1. Internal DSLs in Scala Zef Hemel Medewerker no. 0855447
  2. 2. Java++
  3. 3. header("Add entry") form { table { row { col { text("Your name:") } col { newEntry.name = input(newEntry.name) } } row { col { text("Your message:") } col { newEntry.text = inputText(newEntry.text) } } } button("Post") { newEntry.save() goto(Home()) } }
  4. 4. in Scala
  5. 5. Pages
  6. 6. case class Home() extends Page { def ui { header("Welcome to my guestbook!") section { header("All entries") list { for (e <- cache("entries", Entry.all)) { listitem { form { text(e.name) text(": ") text(e.text) button("Delete") { e.delete() goto(Home()) } } } } } } } }
  7. 7. Templates
  8. 8. case class Home() extends Page { def ui { header("Welcome to my guestbook!") section { entries } } def entries { header("All entries") list { for (e <- cache("entries", Entry.all)) { listitem { form { text(e.name) text(": ") text(e.text) button("Delete") { e.delete() goto(Home()) } } } } } } }
  9. 9. abstract class MainTemplate extends Page { def body def ui { block("headerblock") { header { navigate(Home()) {text("My Guestbook")} } } hr body hr text("(C) 2009, Zef Hemel") } }
  10. 10. case class Home() extends MainTemplate { def body { header("Welcome to my guestbook!") section { entries } } def entries { header("All entries") for (e <- cache("entries", Entry.all)) { listitem { form { text(e.name) text(": ") text(e.text) button("Delete") { e.delete() goto(Home()) } } } } } ... }
  11. 11. Styling
  12. 12. object DefaultStyle extends Style { block("headerblock") >> header { fontsize = 30 pt; width = 100 percent; bgcolor = "#eeeeee"; } section >> header { color = "#0c0ccc"; } body { fontfamily = "Helvetica, Arial, Verdana, sans-serif" } }
  13. 13. abstract class MainTemplate extends Page { def body style = DefaultStyle def ui { block("headerblock") { header { navigate(Home()) {text("My Guestbook")} } } hr body hr text("(C) 2009, Zef Hemel") } }
  14. 14. Access Control (added this morning in 15 lines of code)
  15. 15. case class CannotEnter() extends Page with AccessControl { def canAccess = false def ui { text("You should never see this!") } }
  16. 16. Data model
  17. 17. class Entry extends DataObject { def singleton = Entry @Column var name : String = "" @Column var text : String = "" } object Entry extends DataObjectSingleton(classOf[Entry])
  18. 18. How?
  19. 19. OO features basic: classes, objects, inheritance, reflection traits
  20. 20. function programming closures
  21. 21. flexible syntax a >> b == a.>>(b) form { ... } ~= form(() => { ... })
  22. 22. implicits 8 pt == new UnitIntWrapper(8).pt Entry.all == Entry.all(dbSession)
  23. 23. type inference var newEntry = Entry() == var newEntry : Entry = Entry()
  24. 24. Advantages
  25. 25. “languages” as libraries trivial “language” composition
  26. 26. Limitations
  27. 27. Scala → JVM
  28. 28. model checking == type checking
  29. 29. boiler plate case class Home() extends Page { def ui { } }
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×