FUN WITH MACROS
Nadav Wiener
It was Colonel Mustard, in the
lounge room, with a rope
The Suspects
sealed trait Suspect	
case object MissScarlet extends Suspect	
case object ColonelMustard extends Suspect	
ca...
sealed trait Weapon	
case object Candlestick extends Weapon	
case object Dagger extends Weapon	
case object LeadPipe exten...
The Rooms
sealed trait Room	
case object Kitchen extends Room	
case object Ballroom extends Room	
case object Conservatory...
case class Accusation(	
suspect: Suspect, 	
room: Room, 	
weapon: Weapon)	

The
Accusation
BUT NOW
WHAT?
Simple answer:
val accusation = Accusation(	
ColonelMustard, Library, LeadPipe)	

or build a fancy API
Mixfix notation

fancy
Example
@syntax def _inThe_withThe_	
_inThe_withThe_
(suspect: Suspect, room: Room, weapon: Weapon) = 	
Accusation(suspect, room, ...
@syntax def _inThe_withThe_

(ColonelMustard inThe Library withThe LeadPipe)

the method name is
parsed by the macro
MACROS
FORMULA1
INTRO
object Cowsay {	
def cowsay(text: String): String = macro cowsayImpl	
!

def cowsayImpl(c: Context)	
(text: c.Expr[String]...
println(Cowsay.cowsay("use macros!"))	

_______________
< 'use macros!' >
--------------
^__^
 (oo)_______
(__)
)/
||----w...
Compiled code keeps working
even after removing ‘cowsay’
_______________
< 'use macros!' >
--------------
^__^
 (oo)______...
Available in 2.10
Macro Paradise

TYPE PROVIDERS I:

MACRO
ANNOTATIONS
class syntax extends StaticAnnotation {	
def macroTransform(annottees: Any*) = 	
macro Mixfix.impl	
}

Macro applied using...
object Mixfix {	
def impl(c: Context)	
(annottees: c.Expr[Any]*): c.Expr[Any] = //…	
}

Implemented like other macros
@syntax def _inThe_withThe_(…) = //…
import _inThe_withThe_._	
!

Enables adding new types
and definitions
TYPE PROVIDERS II:

SYNTHETIC
STRUCTURAL TYPES
Structural type:
val foo = new {	
def bar() = println(3)	
}	
	
!

foo.bar()	
!
Structural types:
Type checked

Use reflection at runtime

Can be generated using 2.10 def
macros
ADJECTIVES WITH MACROS
The challenge
Getting from:

case class Book(	
interesting: Boolean = false,	
length: Length = short,	
color: Color = red)...
mkAdjectives extracts adjectives
object Book {	
val adjectives = Adjectives.mkAdjectives[Book]	
}	
import Book.adjectives....
THANK YOU
WE’RE HIRING

contact nadav@lynxguard.com
Macro Paradise

http://docs.scala-lang.org/overviews/macros/paradise.html

Scala...
Mixfix & adjectives using Scala macros
Upcoming SlideShare
Loading in...5
×

Mixfix & adjectives using Scala macros

283

Published on

A short talk about using type providers for fun and profit. (well, mostly fun).

The talk was delivered at Underscore, the Israeli Scala user group.

Video:
http://parleys.com/play/530e2217e4b06b7f61f3fc06

1) Mixfix notation:
going from
@syntax def _inThe_withThe_(...) = ...
to
(ColonelMustard inThe LoungeRoom withThe LeadPipe)
GitHub: https://github.com/hunam/scala-mixfix

2) Use adjectives to describe your objects:
going from
Book(length = long, interesting = true, color = red)
to
a (long, interesting, red) Book
GitHub: https://github.com/hunam/scala-adjectives

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
283
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Mixfix & adjectives using Scala macros

  1. 1. FUN WITH MACROS Nadav Wiener
  2. 2. It was Colonel Mustard, in the lounge room, with a rope
  3. 3. The Suspects sealed trait Suspect case object MissScarlet extends Suspect case object ColonelMustard extends Suspect case object MrsWhite extends Suspect case object ReverendGreen extends Suspect case object MrsPeacock extends Suspect case object ProfessorPlum extends Suspect
  4. 4. sealed trait Weapon case object Candlestick extends Weapon case object Dagger extends Weapon case object LeadPipe extends Weapon case object Revolver extends Weapon case object Rope extends Weapon case object Wrench extends Weapon The Weapons
  5. 5. The Rooms sealed trait Room case object Kitchen extends Room case object Ballroom extends Room case object Conservatory extends Room case object DiningRoom extends Room case object BilliardRoom extends Room case object Library extends Room case object Lounge extends Room case object Hall extends Room case object Study extends Room
  6. 6. case class Accusation( suspect: Suspect, room: Room, weapon: Weapon) The Accusation
  7. 7. BUT NOW WHAT?
  8. 8. Simple answer: val accusation = Accusation( ColonelMustard, Library, LeadPipe) or build a fancy API
  9. 9. Mixfix notation fancy
  10. 10. Example
  11. 11. @syntax def _inThe_withThe_ _inThe_withThe_ (suspect: Suspect, room: Room, weapon: Weapon) = Accusation(suspect, room, weapon) ! import _inThe_withThe_._ ! ! (ColonelMustard inThe Library withThe LeadPipe) == accusation Why the awkward name?
  12. 12. @syntax def _inThe_withThe_ (ColonelMustard inThe Library withThe LeadPipe) the method name is parsed by the macro
  13. 13. MACROS FORMULA1 INTRO
  14. 14. object Cowsay { def cowsay(text: String): String = macro cowsayImpl ! def cowsayImpl(c: Context) (text: c.Expr[String]): c.Expr[String] = { ! import c.universe._ import sys.process._ ! val Literal(Constant(input)) = text.tree val output = s"cowsay '$input'"!! c.Expr(q"$output") } }
  15. 15. println(Cowsay.cowsay("use macros!")) _______________ < 'use macros!' > -------------- ^__^ (oo)_______ (__) )/ ||----w | || ||
  16. 16. Compiled code keeps working even after removing ‘cowsay’ _______________ < 'use macros!' > -------------- ^__^ (oo)_______ (__) )/ ||----w | || ||
  17. 17. Available in 2.10 Macro Paradise TYPE PROVIDERS I:
 MACRO ANNOTATIONS
  18. 18. class syntax extends StaticAnnotation { def macroTransform(annottees: Any*) = macro Mixfix.impl } Macro applied using @syntax
  19. 19. object Mixfix { def impl(c: Context) (annottees: c.Expr[Any]*): c.Expr[Any] = //… } Implemented like other macros
  20. 20. @syntax def _inThe_withThe_(…) = //… import _inThe_withThe_._ ! Enables adding new types and definitions
  21. 21. TYPE PROVIDERS II:
 SYNTHETIC STRUCTURAL TYPES
  22. 22. Structural type: val foo = new { def bar() = println(3) } ! foo.bar() !
  23. 23. Structural types: Type checked Use reflection at runtime Can be generated using 2.10 def macros
  24. 24. ADJECTIVES WITH MACROS
  25. 25. The challenge Getting from: case class Book( interesting: Boolean = false, length: Length = short, color: Color = red) Book(true, short, red) To: an (interesting, long, red) Book
  26. 26. mkAdjectives extracts adjectives object Book { val adjectives = Adjectives.mkAdjectives[Book] } import Book.adjectives._ ! println(an (interesting, long, red) Book) ! Literate initialization for finite fields: boolean, enumeration, case objects Inspired by E
  27. 27. THANK YOU
  28. 28. WE’RE HIRING contact nadav@lynxguard.com Macro Paradise
 http://docs.scala-lang.org/overviews/macros/paradise.html Scala Adjectives
 https://github.com/hunam/scala-adjectives Scala Mixfix
 https://github.com/hunam/scala-mixfix
  1. A particular slide catching your eye?

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

×