mixfix & adjectives using scala macros

29
FUN WITH MACROS Nadav Wiener

Upload: nadav-wiener

Post on 04-Dec-2014

334 views

Category:

Technology


2 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Mixfix & adjectives using Scala macros

FUN WITH MACROSNadav Wiener

Page 2: Mixfix & adjectives using Scala macros
Page 3: Mixfix & adjectives using Scala macros

It was Colonel Mustard, in the lounge room, with a rope

Page 4: Mixfix & adjectives using Scala macros

sealed trait Suspectcase object MissScarlet extends Suspectcase object ColonelMustard extends Suspectcase object MrsWhite extends Suspectcase object ReverendGreen extends Suspectcase object MrsPeacock extends Suspectcase object ProfessorPlum extends Suspect

The Suspects

Page 5: Mixfix & adjectives using Scala macros

The Weapons

sealed trait Weaponcase object Candlestick extends Weaponcase object Dagger extends Weaponcase object LeadPipe extends Weaponcase object Revolver extends Weaponcase object Rope extends Weaponcase object Wrench extends Weapon

Page 6: Mixfix & adjectives using Scala macros

The Rooms

sealed trait Roomcase object Kitchen extends Roomcase object Ballroom extends Roomcase object Conservatory extends Roomcase object DiningRoom extends Roomcase object BilliardRoom extends Roomcase object Library extends Roomcase object Lounge extends Roomcase object Hall extends Roomcase object Study extends Room

Page 7: Mixfix & adjectives using Scala macros

The Accusation

case class Accusation( suspect: Suspect, room: Room, weapon: Weapon)

Page 8: Mixfix & adjectives using Scala macros

BUT NOW WHAT?

Page 9: Mixfix & adjectives using Scala macros

Simple answer:val accusation = Accusation( ColonelMustard, Library, LeadPipe)

or build a fancy API

Page 10: Mixfix & adjectives using Scala macros

Mixfix notation ⇒ fancy

Page 11: Mixfix & adjectives using Scala macros

Example

Page 12: Mixfix & adjectives using Scala macros

@syntax def _inThe_withThe_ (suspect: Suspect, room: Room, weapon: Weapon) = Accusation(suspect, room, weapon) !import _inThe_withThe_._!!(ColonelMustard inThe Library withThe LeadPipe) == accusation

@syntax def _inThe_withThe_

(ColonelMustard inThe Library withThe LeadPipe)

Why the awkward name?

Page 13: Mixfix & adjectives using Scala macros

@syntax def _inThe_withThe_

(ColonelMustard inThe Library withThe LeadPipe)

the method name is parsed by the macro

Page 14: Mixfix & adjectives using Scala macros

MACROS FORMULA1

INTRO

Page 15: Mixfix & adjectives using Scala macros

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") }}

Page 16: Mixfix & adjectives using Scala macros

println(Cowsay.cowsay("use macros!"))

_______________ < 'use macros!' > --------------- \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || ||

_______________ < 'use macros!' > --------------- \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || ||

Page 17: Mixfix & adjectives using Scala macros

_______________ < 'use macros!' > --------------- \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || ||

Compiled code keeps working even after removing ‘cowsay’

Page 18: Mixfix & adjectives using Scala macros

TYPE PROVIDERS I:MACRO

ANNOTATIONS

Available in 2.10 Macro Paradise

Page 19: Mixfix & adjectives using Scala macros

Macro applied using @syntax

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

Page 20: Mixfix & adjectives using Scala macros

Implemented like other macros

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

Page 21: Mixfix & adjectives using Scala macros

Enables adding new types and definitions

@syntax def _inThe_withThe_(…) = //…

import _inThe_withThe_._!

Page 22: Mixfix & adjectives using Scala macros

TYPE PROVIDERS II: SYNTHETIC

STRUCTURAL TYPES

Page 23: Mixfix & adjectives using Scala macros

val foo = new { def bar() = println(3)} !foo.bar()!

Structural type:

Page 24: Mixfix & adjectives using Scala macros

Type checked

Use reflection at runtime

Can be generated using 2.10 def macros

Structural types:

Page 25: Mixfix & adjectives using Scala macros

ADJECTIVES WITH MACROS

Page 26: Mixfix & adjectives using Scala macros

The challenge

Book(true, short, red)

Getting from:

To:

case class Book( interesting: Boolean = false, length: Length = short, color: Color = red)

an (interesting, long, red) Book

Page 27: Mixfix & adjectives using Scala macros

mkAdjectives extracts adjectives

Literate initialization for finite fields: boolean, enumeration, case objects

Inspired by E

object Book { val adjectives = Adjectives.mkAdjectives[Book]}import Book.adjectives._!println(an (interesting, long, red) Book)!

Page 28: Mixfix & adjectives using Scala macros

THANK YOU

Page 29: Mixfix & adjectives using Scala macros

Macro Paradisehttp://docs.scala-lang.org/overviews/macros/paradise.htmlScala Adjectiveshttps://github.com/hunam/scala-adjectivesScala Mixfixhttps://github.com/hunam/scala-mixfix

WE’RE HIRING contact [email protected]