macros and reflection in scala 2.10

20
Macros & reflection in Scala 2.10

Upload: johan-andren

Post on 04-Dec-2014

1.483 views

Category:

Technology


1 download

DESCRIPTION

My Topconf Tallinn 2012 talk on macros and reflection in Scala 2.10.

TRANSCRIPT

Page 1: Macros and reflection in scala 2.10

Macros & reflection

in Scala 2.10

Page 2: Macros and reflection in scala 2.10

AboutConsultant

JVM/Java - 7 years

Scala - 1 year

me

@apnylle

Page 3: Macros and reflection in scala 2.10

IMPORTANT WARNING

The reflection/macros in Scala 2.10 are experimental and pretty complex, you might shoot yourself in the foot or worse and be the first one to do it.

(They are also totally awesome)

Page 4: Macros and reflection in scala 2.10

What

is re

flec

tion?

Photo: Christian Holmér

?

Page 5: Macros and reflection in scala 2.10

What

is re

flec

tion?

... reflection is the ability of a computer program to examine ... and modify the structure and behavior ... of an object at runtime.

”wikipedia

Page 6: Macros and reflection in scala 2.10

Hasn’t Scala already got the Java reflection API?!

No support for Scala constructs!

Type erasure

List[Int] + compiler = List :(

B!" ...

Page 7: Macros and reflection in scala 2.10

w# w$%" &'(#")*%+ b#""#r!

Homoiconic(ness) - express the language in itself

One API for reflection, macros (and the

compiler)

Support for reification

No overhead when not used

Also:

Page 8: Macros and reflection in scala 2.10

Let’s Look At Some CodeT$,-*%+ *& .)#$/...

Page 9: Macros and reflection in scala 2.10

What about macros?

A macro (from the Greek μακρό for "big" or "far") in computer science is a rule or pattern that specifies how a certain input sequence (often a sequence of characters) should be mapped to a replacement input sequence (also often a sequence of characters) according to a defined procedure.

”wikipedia

Wat!?!

Page 10: Macros and reflection in scala 2.10

Ok, I Get it

A regular scala function

AST => AST

Called by the compiler

b!" w)$" *& $ ($.r', r#$,,0?

Page 11: Macros and reflection in scala 2.10

Ok, I Get it&' w)$" *& $% AST, r#$,,0?

if(1 == ”1”) { true } else { false }

if(1.==(”1”)) { true } else { false }

if(1.$eq$eq(”1”)) { true } else { false }

Page 12: Macros and reflection in scala 2.10

If

if(1.$eq$eq(”1”)) { true } else { false }

Apply

Select

Literal

Constant(1)

Name(”$eq$eq”)Literal

Constant(”1”)

List

Constant(false)

LiteralLiteral

Constant(true)

Page 13: Macros and reflection in scala 2.10

If

Expr Expr Expr

case class If(cond: Expr, then: Expr, else: Expr)

if(1.$eq$eq(”1”)) { true } else { false }

Page 14: Macros and reflection in scala 2.10

Transforming an AST to a new AST

w)$" 1' w# 1' w*") *"?

S', W# )$v# $ 2%."*'% r!%%*%+ $" .'(/*,# "*(#

Page 15: Macros and reflection in scala 2.10

Ideas for macros

Compile time checking of strings

Generic no-runtime-overhead toString

Super efficient loops

zero-overhead logger, assert

'!" 'f ")# "'/ 'f (0 )#$1

Page 16: Macros and reflection in scala 2.10

Ideas for macros

def myMacro(expr: Expr): Expr {

if (expr != valid dateformat) throw Error

else expr

}

.'(/*,# "*(# .)#.-*%+ 'f &"r*%+&

Page 17: Macros and reflection in scala 2.10

Ideas for macros

def toStringMacro[T](expr: Expr[T]): Expr[String] {

expr for class name of T +

each field with public getter

}

+#%#r*. "'S"r*%+ w*") z#r'-'v#r)#$1

Page 18: Macros and reflection in scala 2.10

show some

(Awesome) macros

3(# "'

Finally!(I stopped listening

five slides

ago)

Photo: Paul Lewis

Page 20: Macros and reflection in scala 2.10

S!"#$%: topconf.com / the program page

S&'(!$ (r)*$+,: http://github.com/johanandren/macros-2012

'&"!: [email protected]

,w",,$r: @apnylle

Questions?