SlideShare une entreprise Scribd logo
1  sur  30
Télécharger pour lire hors ligne
Introduction to
meta-programming in Scala
Alessandro Marrella
About me
● Software Engineer
● Working with Scala professionally for 3
years
● ♡ automating stuff
● ♡ functional programming
● github.com/amarrella
● linkedin.com/in/alessandromarrella
CREDITS
● Ólafur Páll Geirsson (@olafurpg)
● Scala Center / EPFL (scala.epfl.ch)
● Twitter
+ many other contributors in the Scala
community
Meta-programming
● Treating other programs as Data
● Ability to
○ Read
○ Generate
other programs
WHY?
● Reduce lines of code & dev time
● Reduce boilerplate, errors and boring stuff
○ Transform
○ Analyze
Scalameta
● Library to read, analyze, transform and generate Scala
programs
○ Syntactic API: parse, transform & prettyprint
○ Semantic API: understand symbols and types
● Ecosystem
○ Scalameta (Trees & SemanticDB)
○ Scalafmt: code formatting
○ Scalafix: code linting & rewriting
○ Metals: language server
& more( scalameta.org/docs/misc/built-with-scalameta.html )
Syntax trees
Syntax trees
1
Lit.Int(1)
Syntax trees
1+1
Lit.Int(1)Lit.Int(1)
Term.ApplyInfix
Term.Name("+")
Syntax trees
val x: Int = 1+1
Lit.Int(1)Lit.Int(1)
Term.ApplyInfix
Term.Name("+")
Defn.Val
Patn.Var
Term.Name(“x”)
Type.Name
Syntax trees
object Main { val x: Int = 1+1 }
Lit.Int(1)Lit.Int(1)
Term.ApplyInfix
Term.Name("+")
Defn.Val
Patn.Var
Term.Name(“x”)
TemplateTerm.Name(“Main”)
Defn.Object
Type.Name
Syntax trees: from tree to code
object Main { }
Lit.Int(1)Lit.Int(1)
Term.ApplyInfix
Term.Name("+")
Defn.Val
Patn.Var
Term.Name(“x”)
TemplateTerm.Name(“Main”)
Defn.Object
Type.Name
Syntax trees: from tree to code
object Main { val x: Int }
Lit.Int(1)Lit.Int(1)
Term.ApplyInfix
Term.Name("+")
Defn.Val
Patn.Var
Term.Name(“x”)
TemplateTerm.Name(“Main”)
Defn.Object
Type.Name
Syntax trees: from tree to code
object Main { val x: Int = 1 + 1 }
Lit.Int(1)Lit.Int(1)
Term.ApplyInfix
Term.Name("+")
Defn.Val
Patn.Var
Term.Name(“x”)
TemplateTerm.Name(“Main”)
Defn.Object
Type.Name
Scalameta
● Scalameta Trees are lossless
● Scalafmt: uses trees to figure out how to
format
● Scalafix: uses trees to apply syntactic
rules (more later)
● Explicit syntax or quasiquotes
q”object $name { val $val: $t = $expr }”
https://scalameta.org/docs/trees/guide.html
SemanticDB
● Compiles scala
programs to
semantic
information
● Decouples producing
and consuming
semantic
information
SemanticDB
package com.alessandromarrella.presentation
import cats.effect.IO
object IOExample {
val x = IO(println("Hello world"))
}
SemanticDB Summary:
Schema => SemanticDB v4
Uri =>
src/main/scala/com/alessandro
marrella/presentation/IOExamp
le.scala
Text => empty
Language => Scala
Symbols => 2 entries
Occurrences => 11 entries
package com.alessandromarrella.presentation
import cats.effect.IO
object IOExample {
val x = IO(println("Hello world"))
}
SemanticDB
Symbols:
com/alessandromarrella/presen
tation/IOExample. => final
object IOExample extends
AnyRef { +1 decls }
com/alessandromarrella/presen
tation/IOExample.x. => val
method x: IO[Unit]
package com.alessandromarrella.presentation
import cats.effect.IO
object IOExample {
val x = IO(println("Hello world"))
}
SemanticDB
Occurrences:
[0:8..0:11) => com/
[0:12..0:30) =>
com/alessandromarrella/
[0:31..0:43) =>
com/alessandromarrella/presen
tation/
package com.alessandromarrella.presentation
import cats.effect.IO
object IOExample {
val x = IO(println("Hello world"))
}
position
SemanticDB
Occurrences:
[2:7..2:11) => cats/
[2:12..2:18) => cats/effect/
[2:19..2:21) =>
cats/effect/IO#
[2:19..2:21) =>
cats/effect/IO.
package com.alessandromarrella.presentation
import cats.effect.IO
object IOExample {
val x = IO(println("Hello world"))
}
<- class
<- object
SemanticDB Occurrences:
[4:7..4:16) <=
com/alessandromarrella/presen
tation/IOExample.
[6:8..6:9) <=
com/alessandromarrella/presen
tation/IOExample.x.
[6:12..6:14) =>
cats/effect/IO.
[6:15..6:22) =>
scala/Predef.println(+1).
package com.alessandromarrella.presentation
import cats.effect.IO
object IOExample {
val x = IO(println("Hello world"))
}
It “knows”
Method overload
SemanticDB
● Compiles the program to a SemanticDB file
● Symbol information
● Disambiguation
● Scalafix can use it for semantic rules
(more later)
● Metals uses it for code navigation
https://scalameta.org/docs/semanticdb/guide.html
Scalafix
● Refactoring and linting tool
● Syntactic and semantic rules
● As a user:
○ Apply rules
○ Integrate with your CI
● As a dev:
○ Create & publish rules
Applying rules
Add scalafix dependency
project/plugins.sbt
addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.9.1")
Run the rule (e.g. http4s 0.18 to 0.20 upgrade)
$ sbt ";scalafixEnable; scalafix github:http4s/http4s/v0_20"
Creating new rules
Create a new project with the Giter8 template
$ sbt new scalacenter/scalafix.g8
You get a new directory with:
build.sbt readme.md input output rules test
Example: No return Unit rule
def foo(x: Int): Unit
What does calling foo(42) do?
Example: No return Unit rule
def foo(x: Int): Unit
What does calling foo(42) do?
● Nothing
● Performs a side effect
Example: No return Unit rule
def foo(x: Int): Unit
What does calling foo(42) do?
● Nothing
● Performs a side effect
We won’t consider:
● Throwing exceptions
● Returning null
We don’t want the user to be able to return Unit.
Example: No return Unit rule
Demo
https://github.com/amarrella/noreturnunit
Thank you!
hello@alessandromarrella.com
linkedin.com/in/alessandromarrella
twitter.com/amarrella

Contenu connexe

Tendances

Scala.js: Next generation front end development in Scala
Scala.js:  Next generation front end development in ScalaScala.js:  Next generation front end development in Scala
Scala.js: Next generation front end development in ScalaOtto Chrons
 
Be a 10x android developer
Be a 10x android developer Be a 10x android developer
Be a 10x android developer Shadaï ALI
 
Counters with Riak on Amazon EC2 at Hackover
Counters with Riak on Amazon EC2 at HackoverCounters with Riak on Amazon EC2 at Hackover
Counters with Riak on Amazon EC2 at HackoverAndrei Savu
 
Jug Marche: Meeting June 2014. Java 8 hands on
Jug Marche: Meeting June 2014. Java 8 hands onJug Marche: Meeting June 2014. Java 8 hands on
Jug Marche: Meeting June 2014. Java 8 hands onOnofrio Panzarino
 
Scala Refactoring for Fun and Profit
Scala Refactoring for Fun and ProfitScala Refactoring for Fun and Profit
Scala Refactoring for Fun and ProfitTomer Gabel
 
Using akka streams to access s3 objects
Using akka streams to access s3 objectsUsing akka streams to access s3 objects
Using akka streams to access s3 objectsMikhail Girkin
 
Scala on-android
Scala on-androidScala on-android
Scala on-androidlifecoder
 
Presentation on functional data mining at the IGT Cloud meet up at eBay Netanya
Presentation on functional data mining at the IGT Cloud meet up at eBay NetanyaPresentation on functional data mining at the IGT Cloud meet up at eBay Netanya
Presentation on functional data mining at the IGT Cloud meet up at eBay NetanyaChristopher Severs
 
Streams or Loops? Java 8 Stream API by Niki Petkov - Proxiad Bulgaria
Streams or Loops? Java 8 Stream API by Niki Petkov - Proxiad BulgariaStreams or Loops? Java 8 Stream API by Niki Petkov - Proxiad Bulgaria
Streams or Loops? Java 8 Stream API by Niki Petkov - Proxiad BulgariaHackBulgaria
 
Haskell for Scala-ists
Haskell for Scala-istsHaskell for Scala-ists
Haskell for Scala-istschriseidhof
 
Educational slides by venay magen
Educational slides by venay magenEducational slides by venay magen
Educational slides by venay magenvenaymagen19
 
Collections Framework Begineers guide 2
Collections Framework Begineers guide 2Collections Framework Begineers guide 2
Collections Framework Begineers guide 2Kenji HASUNUMA
 
ECMAScript 6: A Better JavaScript for the Ambient Computing Era
ECMAScript 6: A Better JavaScript for the Ambient Computing EraECMAScript 6: A Better JavaScript for the Ambient Computing Era
ECMAScript 6: A Better JavaScript for the Ambient Computing EraAllen Wirfs-Brock
 
Using Onyx in anger
Using Onyx in angerUsing Onyx in anger
Using Onyx in angerSimon Belak
 
Building Mobile Apps with Android
Building Mobile Apps with AndroidBuilding Mobile Apps with Android
Building Mobile Apps with AndroidKurt Renzo Acosta
 
LINEデリマでのElasticsearchの運用と監視の話
LINEデリマでのElasticsearchの運用と監視の話LINEデリマでのElasticsearchの運用と監視の話
LINEデリマでのElasticsearchの運用と監視の話LINE Corporation
 
Luis Atencio on RxJS
Luis Atencio on RxJSLuis Atencio on RxJS
Luis Atencio on RxJSLuis Atencio
 
Introduction To Scala
Introduction To ScalaIntroduction To Scala
Introduction To ScalaBasuk
 

Tendances (20)

Scala.js: Next generation front end development in Scala
Scala.js:  Next generation front end development in ScalaScala.js:  Next generation front end development in Scala
Scala.js: Next generation front end development in Scala
 
Scala cheatsheet
Scala cheatsheetScala cheatsheet
Scala cheatsheet
 
Be a 10x android developer
Be a 10x android developer Be a 10x android developer
Be a 10x android developer
 
Counters with Riak on Amazon EC2 at Hackover
Counters with Riak on Amazon EC2 at HackoverCounters with Riak on Amazon EC2 at Hackover
Counters with Riak on Amazon EC2 at Hackover
 
Jug Marche: Meeting June 2014. Java 8 hands on
Jug Marche: Meeting June 2014. Java 8 hands onJug Marche: Meeting June 2014. Java 8 hands on
Jug Marche: Meeting June 2014. Java 8 hands on
 
Scala Refactoring for Fun and Profit
Scala Refactoring for Fun and ProfitScala Refactoring for Fun and Profit
Scala Refactoring for Fun and Profit
 
Onyx
OnyxOnyx
Onyx
 
Using akka streams to access s3 objects
Using akka streams to access s3 objectsUsing akka streams to access s3 objects
Using akka streams to access s3 objects
 
Scala on-android
Scala on-androidScala on-android
Scala on-android
 
Presentation on functional data mining at the IGT Cloud meet up at eBay Netanya
Presentation on functional data mining at the IGT Cloud meet up at eBay NetanyaPresentation on functional data mining at the IGT Cloud meet up at eBay Netanya
Presentation on functional data mining at the IGT Cloud meet up at eBay Netanya
 
Streams or Loops? Java 8 Stream API by Niki Petkov - Proxiad Bulgaria
Streams or Loops? Java 8 Stream API by Niki Petkov - Proxiad BulgariaStreams or Loops? Java 8 Stream API by Niki Petkov - Proxiad Bulgaria
Streams or Loops? Java 8 Stream API by Niki Petkov - Proxiad Bulgaria
 
Haskell for Scala-ists
Haskell for Scala-istsHaskell for Scala-ists
Haskell for Scala-ists
 
Educational slides by venay magen
Educational slides by venay magenEducational slides by venay magen
Educational slides by venay magen
 
Collections Framework Begineers guide 2
Collections Framework Begineers guide 2Collections Framework Begineers guide 2
Collections Framework Begineers guide 2
 
ECMAScript 6: A Better JavaScript for the Ambient Computing Era
ECMAScript 6: A Better JavaScript for the Ambient Computing EraECMAScript 6: A Better JavaScript for the Ambient Computing Era
ECMAScript 6: A Better JavaScript for the Ambient Computing Era
 
Using Onyx in anger
Using Onyx in angerUsing Onyx in anger
Using Onyx in anger
 
Building Mobile Apps with Android
Building Mobile Apps with AndroidBuilding Mobile Apps with Android
Building Mobile Apps with Android
 
LINEデリマでのElasticsearchの運用と監視の話
LINEデリマでのElasticsearchの運用と監視の話LINEデリマでのElasticsearchの運用と監視の話
LINEデリマでのElasticsearchの運用と監視の話
 
Luis Atencio on RxJS
Luis Atencio on RxJSLuis Atencio on RxJS
Luis Atencio on RxJS
 
Introduction To Scala
Introduction To ScalaIntroduction To Scala
Introduction To Scala
 

Similaire à Introduction to meta-programming in scala

BASE Meetup: "Analysing Scala Puzzlers: Essential and Accidental Complexity i...
BASE Meetup: "Analysing Scala Puzzlers: Essential and Accidental Complexity i...BASE Meetup: "Analysing Scala Puzzlers: Essential and Accidental Complexity i...
BASE Meetup: "Analysing Scala Puzzlers: Essential and Accidental Complexity i...Andrew Phillips
 
Scala Up North: "Analysing Scala Puzzlers: Essential and Accidental Complexit...
Scala Up North: "Analysing Scala Puzzlers: Essential and Accidental Complexit...Scala Up North: "Analysing Scala Puzzlers: Essential and Accidental Complexit...
Scala Up North: "Analysing Scala Puzzlers: Essential and Accidental Complexit...Andrew Phillips
 
New features in jdk8 iti
New features in jdk8 itiNew features in jdk8 iti
New features in jdk8 itiAhmed mar3y
 
From Java to Scala - advantages and possible risks
From Java to Scala - advantages and possible risksFrom Java to Scala - advantages and possible risks
From Java to Scala - advantages and possible risksSeniorDevOnly
 
Kotlin boost yourproductivity
Kotlin boost yourproductivityKotlin boost yourproductivity
Kotlin boost yourproductivitynklmish
 
Develop realtime web with Scala and Xitrum
Develop realtime web with Scala and XitrumDevelop realtime web with Scala and Xitrum
Develop realtime web with Scala and XitrumNgoc Dao
 
Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Martin Odersky
 
Dax Declarative Api For Xml
Dax   Declarative Api For XmlDax   Declarative Api For Xml
Dax Declarative Api For XmlLars Trieloff
 
Introduction to clojure
Introduction to clojureIntroduction to clojure
Introduction to clojureAbbas Raza
 
Scala final ppt vinay
Scala final ppt vinayScala final ppt vinay
Scala final ppt vinayViplav Jain
 
What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)Pavlo Baron
 
Dart the Better JavaScript
Dart the Better JavaScriptDart the Better JavaScript
Dart the Better JavaScriptJorg Janke
 
Scala is java8.next()
Scala is java8.next()Scala is java8.next()
Scala is java8.next()daewon jeong
 
Hierarchical free monads and software design in fp
Hierarchical free monads and software design in fpHierarchical free monads and software design in fp
Hierarchical free monads and software design in fpAlexander Granin
 
Logstash-Elasticsearch-Kibana
Logstash-Elasticsearch-KibanaLogstash-Elasticsearch-Kibana
Logstash-Elasticsearch-Kibanadknx01
 

Similaire à Introduction to meta-programming in scala (20)

What`s New in Java 8
What`s New in Java 8What`s New in Java 8
What`s New in Java 8
 
BASE Meetup: "Analysing Scala Puzzlers: Essential and Accidental Complexity i...
BASE Meetup: "Analysing Scala Puzzlers: Essential and Accidental Complexity i...BASE Meetup: "Analysing Scala Puzzlers: Essential and Accidental Complexity i...
BASE Meetup: "Analysing Scala Puzzlers: Essential and Accidental Complexity i...
 
Scala Up North: "Analysing Scala Puzzlers: Essential and Accidental Complexit...
Scala Up North: "Analysing Scala Puzzlers: Essential and Accidental Complexit...Scala Up North: "Analysing Scala Puzzlers: Essential and Accidental Complexit...
Scala Up North: "Analysing Scala Puzzlers: Essential and Accidental Complexit...
 
New features in jdk8 iti
New features in jdk8 itiNew features in jdk8 iti
New features in jdk8 iti
 
Scala ntnu
Scala ntnuScala ntnu
Scala ntnu
 
SCALA - Functional domain
SCALA -  Functional domainSCALA -  Functional domain
SCALA - Functional domain
 
From Java to Scala - advantages and possible risks
From Java to Scala - advantages and possible risksFrom Java to Scala - advantages and possible risks
From Java to Scala - advantages and possible risks
 
Kotlin boost yourproductivity
Kotlin boost yourproductivityKotlin boost yourproductivity
Kotlin boost yourproductivity
 
Scala coated JVM
Scala coated JVMScala coated JVM
Scala coated JVM
 
Develop realtime web with Scala and Xitrum
Develop realtime web with Scala and XitrumDevelop realtime web with Scala and Xitrum
Develop realtime web with Scala and Xitrum
 
Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009
 
Dax Declarative Api For Xml
Dax   Declarative Api For XmlDax   Declarative Api For Xml
Dax Declarative Api For Xml
 
Introduction to clojure
Introduction to clojureIntroduction to clojure
Introduction to clojure
 
Scala final ppt vinay
Scala final ppt vinayScala final ppt vinay
Scala final ppt vinay
 
What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)
 
Dart the Better JavaScript
Dart the Better JavaScriptDart the Better JavaScript
Dart the Better JavaScript
 
Flink internals web
Flink internals web Flink internals web
Flink internals web
 
Scala is java8.next()
Scala is java8.next()Scala is java8.next()
Scala is java8.next()
 
Hierarchical free monads and software design in fp
Hierarchical free monads and software design in fpHierarchical free monads and software design in fp
Hierarchical free monads and software design in fp
 
Logstash-Elasticsearch-Kibana
Logstash-Elasticsearch-KibanaLogstash-Elasticsearch-Kibana
Logstash-Elasticsearch-Kibana
 

Dernier

why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendArshad QA
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 

Dernier (20)

why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and Backend
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 

Introduction to meta-programming in scala

  • 1. Introduction to meta-programming in Scala Alessandro Marrella
  • 2. About me ● Software Engineer ● Working with Scala professionally for 3 years ● ♡ automating stuff ● ♡ functional programming ● github.com/amarrella ● linkedin.com/in/alessandromarrella
  • 3. CREDITS ● Ólafur Páll Geirsson (@olafurpg) ● Scala Center / EPFL (scala.epfl.ch) ● Twitter + many other contributors in the Scala community
  • 4. Meta-programming ● Treating other programs as Data ● Ability to ○ Read ○ Generate other programs WHY? ● Reduce lines of code & dev time ● Reduce boilerplate, errors and boring stuff ○ Transform ○ Analyze
  • 5. Scalameta ● Library to read, analyze, transform and generate Scala programs ○ Syntactic API: parse, transform & prettyprint ○ Semantic API: understand symbols and types ● Ecosystem ○ Scalameta (Trees & SemanticDB) ○ Scalafmt: code formatting ○ Scalafix: code linting & rewriting ○ Metals: language server & more( scalameta.org/docs/misc/built-with-scalameta.html )
  • 9. Syntax trees val x: Int = 1+1 Lit.Int(1)Lit.Int(1) Term.ApplyInfix Term.Name("+") Defn.Val Patn.Var Term.Name(“x”) Type.Name
  • 10. Syntax trees object Main { val x: Int = 1+1 } Lit.Int(1)Lit.Int(1) Term.ApplyInfix Term.Name("+") Defn.Val Patn.Var Term.Name(“x”) TemplateTerm.Name(“Main”) Defn.Object Type.Name
  • 11. Syntax trees: from tree to code object Main { } Lit.Int(1)Lit.Int(1) Term.ApplyInfix Term.Name("+") Defn.Val Patn.Var Term.Name(“x”) TemplateTerm.Name(“Main”) Defn.Object Type.Name
  • 12. Syntax trees: from tree to code object Main { val x: Int } Lit.Int(1)Lit.Int(1) Term.ApplyInfix Term.Name("+") Defn.Val Patn.Var Term.Name(“x”) TemplateTerm.Name(“Main”) Defn.Object Type.Name
  • 13. Syntax trees: from tree to code object Main { val x: Int = 1 + 1 } Lit.Int(1)Lit.Int(1) Term.ApplyInfix Term.Name("+") Defn.Val Patn.Var Term.Name(“x”) TemplateTerm.Name(“Main”) Defn.Object Type.Name
  • 14. Scalameta ● Scalameta Trees are lossless ● Scalafmt: uses trees to figure out how to format ● Scalafix: uses trees to apply syntactic rules (more later) ● Explicit syntax or quasiquotes q”object $name { val $val: $t = $expr }” https://scalameta.org/docs/trees/guide.html
  • 15. SemanticDB ● Compiles scala programs to semantic information ● Decouples producing and consuming semantic information
  • 17. SemanticDB Summary: Schema => SemanticDB v4 Uri => src/main/scala/com/alessandro marrella/presentation/IOExamp le.scala Text => empty Language => Scala Symbols => 2 entries Occurrences => 11 entries package com.alessandromarrella.presentation import cats.effect.IO object IOExample { val x = IO(println("Hello world")) }
  • 18. SemanticDB Symbols: com/alessandromarrella/presen tation/IOExample. => final object IOExample extends AnyRef { +1 decls } com/alessandromarrella/presen tation/IOExample.x. => val method x: IO[Unit] package com.alessandromarrella.presentation import cats.effect.IO object IOExample { val x = IO(println("Hello world")) }
  • 19. SemanticDB Occurrences: [0:8..0:11) => com/ [0:12..0:30) => com/alessandromarrella/ [0:31..0:43) => com/alessandromarrella/presen tation/ package com.alessandromarrella.presentation import cats.effect.IO object IOExample { val x = IO(println("Hello world")) } position
  • 20. SemanticDB Occurrences: [2:7..2:11) => cats/ [2:12..2:18) => cats/effect/ [2:19..2:21) => cats/effect/IO# [2:19..2:21) => cats/effect/IO. package com.alessandromarrella.presentation import cats.effect.IO object IOExample { val x = IO(println("Hello world")) } <- class <- object
  • 21. SemanticDB Occurrences: [4:7..4:16) <= com/alessandromarrella/presen tation/IOExample. [6:8..6:9) <= com/alessandromarrella/presen tation/IOExample.x. [6:12..6:14) => cats/effect/IO. [6:15..6:22) => scala/Predef.println(+1). package com.alessandromarrella.presentation import cats.effect.IO object IOExample { val x = IO(println("Hello world")) } It “knows” Method overload
  • 22. SemanticDB ● Compiles the program to a SemanticDB file ● Symbol information ● Disambiguation ● Scalafix can use it for semantic rules (more later) ● Metals uses it for code navigation https://scalameta.org/docs/semanticdb/guide.html
  • 23. Scalafix ● Refactoring and linting tool ● Syntactic and semantic rules ● As a user: ○ Apply rules ○ Integrate with your CI ● As a dev: ○ Create & publish rules
  • 24. Applying rules Add scalafix dependency project/plugins.sbt addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.9.1") Run the rule (e.g. http4s 0.18 to 0.20 upgrade) $ sbt ";scalafixEnable; scalafix github:http4s/http4s/v0_20"
  • 25. Creating new rules Create a new project with the Giter8 template $ sbt new scalacenter/scalafix.g8 You get a new directory with: build.sbt readme.md input output rules test
  • 26. Example: No return Unit rule def foo(x: Int): Unit What does calling foo(42) do?
  • 27. Example: No return Unit rule def foo(x: Int): Unit What does calling foo(42) do? ● Nothing ● Performs a side effect
  • 28. Example: No return Unit rule def foo(x: Int): Unit What does calling foo(42) do? ● Nothing ● Performs a side effect We won’t consider: ● Throwing exceptions ● Returning null We don’t want the user to be able to return Unit.
  • 29. Example: No return Unit rule Demo https://github.com/amarrella/noreturnunit