SlideShare uma empresa Scribd logo
1 de 29
Baixar para ler offline
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	
case object MrsWhite extends Suspect	
case object ReverendGreen extends Suspect	
case object MrsPeacock extends Suspect	
case object ProfessorPlum extends Suspect
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
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
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, weapon)	
	
!

import _inThe_withThe_._	
!
!

(ColonelMustard inThe Library withThe LeadPipe) ==	
accusation

Why the awkward name?
@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]): 	
c.Expr[String] = {	
!

import c.universe._	
import sys.process._	
!

val Literal(Constant(input)) = text.tree	
	
val output = s"cowsay '$input'"!!	
	
c.Expr(q"$output")	
}	
}
println(Cowsay.cowsay("use macros!"))	

_______________
< 'use macros!' >
--------------
^__^
 (oo)_______
(__)
)/
||----w |
||
||
Compiled code keeps working
even after removing ‘cowsay’
_______________
< 'use macros!' >
--------------
^__^
 (oo)_______
(__)
)/
||----w |
||
||
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 @syntax
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)	
Book(true, short, red)

To:


an (interesting, long, red) Book
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
THANK YOU
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

Mais conteúdo relacionado

Semelhante a Mixfix & adjectives using Scala macros

Pharo, an innovative and open-source Smalltalk
Pharo, an innovative and open-source SmalltalkPharo, an innovative and open-source Smalltalk
Pharo, an innovative and open-source Smalltalk
Serge Stinckwich
 
What I learned from Seven Languages in Seven Weeks (IPRUG)
What I learned from Seven Languages in Seven Weeks (IPRUG)What I learned from Seven Languages in Seven Weeks (IPRUG)
What I learned from Seven Languages in Seven Weeks (IPRUG)
Kerry Buckley
 
Ruby 程式語言入門導覽
Ruby 程式語言入門導覽Ruby 程式語言入門導覽
Ruby 程式語言入門導覽
Wen-Tien Chang
 

Semelhante a Mixfix & adjectives using Scala macros (20)

Thea: Processing OWL Ontologies - An application of logic programming
Thea: Processing OWL Ontologies - An application of logic programmingThea: Processing OWL Ontologies - An application of logic programming
Thea: Processing OWL Ontologies - An application of logic programming
 
Processing OWL2 Ontologies using Thea: An application of Logic Programming
Processing OWL2 Ontologies using Thea: An application of Logic ProgrammingProcessing OWL2 Ontologies using Thea: An application of Logic Programming
Processing OWL2 Ontologies using Thea: An application of Logic Programming
 
Pharo, an innovative and open-source Smalltalk
Pharo, an innovative and open-source SmalltalkPharo, an innovative and open-source Smalltalk
Pharo, an innovative and open-source Smalltalk
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard Way
 
What I learned from Seven Languages in Seven Weeks (IPRUG)
What I learned from Seven Languages in Seven Weeks (IPRUG)What I learned from Seven Languages in Seven Weeks (IPRUG)
What I learned from Seven Languages in Seven Weeks (IPRUG)
 
7li7w devcon5
7li7w devcon57li7w devcon5
7li7w devcon5
 
Prolog 01
Prolog 01Prolog 01
Prolog 01
 
Ceylon idioms by Gavin King
Ceylon idioms by Gavin KingCeylon idioms by Gavin King
Ceylon idioms by Gavin King
 
Ruby
RubyRuby
Ruby
 
Introduction to TypeScript
Introduction to TypeScriptIntroduction to TypeScript
Introduction to TypeScript
 
Java Tips, Tricks and Pitfalls
Java Tips, Tricks and PitfallsJava Tips, Tricks and Pitfalls
Java Tips, Tricks and Pitfalls
 
Type Driven Development @ Confitura 2014
Type Driven Development @ Confitura 2014Type Driven Development @ Confitura 2014
Type Driven Development @ Confitura 2014
 
Intro to ruby
Intro to rubyIntro to ruby
Intro to ruby
 
An Introduction to Scala (2014)
An Introduction to Scala (2014)An Introduction to Scala (2014)
An Introduction to Scala (2014)
 
Scala for curious
Scala for curiousScala for curious
Scala for curious
 
The Great Scala Makeover
The Great Scala MakeoverThe Great Scala Makeover
The Great Scala Makeover
 
Scala Workshop
Scala WorkshopScala Workshop
Scala Workshop
 
Scala introduction
Scala introductionScala introduction
Scala introduction
 
scala-101
scala-101scala-101
scala-101
 
Ruby 程式語言入門導覽
Ruby 程式語言入門導覽Ruby 程式語言入門導覽
Ruby 程式語言入門導覽
 

Último

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Último (20)

DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 

Mixfix & adjectives using Scala macros