SlideShare uma empresa Scribd logo
1 de 16
Baixar para ler offline
Refined Types
Tipagem ainda mais forte para Scala!
Marcelo Gomes
• Nasceu na pequena Juiz de Fora - MG

• Morou 9 anos no Rio

• Já mexeu com Delphi, Java, Ruby e
Objective-C

• Agora mora em São Paulo e trabalha
com Scala
Pra quê servem refined types?
• Tipagem forte e estática é muito legal! (com inferência
então…)

• O compilador ajuda a evitar um monte de erros bobos

• Mas na maior parte do tempo, isso não basta…
Exemplo: ônibus de SP
ID
De: 1 0000

Até: 8 9999
Linha
• Três formatos

• 7600-10

• 209P-10

• 6L04-41
Empresa
• Pelo menos 3 caracteres 

• Não pode ser só espaços em branco
case class SpBus(
id: Int,
line: String,
company: String
)
Parece OK, mas…
…algo errado não está certo!
val bus = SpBus(
id = 0,
line = "SV917",
company = ""
)
O compilador vai aceitar essa barbaridade!
Biblioteca "Refined"
ao resgate!
object SpBus {
type IdRange = Interval.Closed[W.`10000`.T, W.`89999`.T]
type LineRegex = MatchesRegex[
W.`"""^[1-8][0-9L][0-9][0-9A-Z]-d{2}$"""`.T]
type MinSizeString =
MinSize[W.`3`.T] And
Not[MatchesRegex[W.`"""^s+$"""`.T]]
}
case class SpBus(
id: Int Refined IdRange,
line: String Refined LineRegex,
company: String Refined MinSizeString
)
Agora, isso não compila mais!
import eu.timepit.refined.auto._
val bus = SpBus(
id = 0,
line = "SV917",
company = “”
)
Para valores não hardcoded, age como validação!
def buildBus(
id: Int,
line: String,
company: String): Either[String,SpBus] = {
for {
id <- refineV[IdRange](id)
line <- refineV[LineRegex](line)
company <- refineV[MinSizeString](company)
} yield SpBus(id, line, company)
}
Não se pode ter tudo na vida…
import eu.timepit.refined.auto._
type NonEmptyPositiveList =
NonEmpty And Forall[Positive]
val x: List[Int] Refined NonEmptyPositiveList =
List(1,2,3,4,5,6,7,8,9)
“compile-time refinement only works with literals”
Mas não perca a esperança!
import eu.timepit.refined.auto._
type NonEmptyValidInts = NonEmpty And Forall[ValidInt]
type AllPositiveInts = Forall[Positive]
type PositiveIntList = List[Int] Refined AllPositiveInts
def parse(s: String): Either[String,PositiveIntList] = {
val validatedStringList =
refineV[NonEmptyValidInts](s.split(",").toList)
val validatedIntList = validatedStringList
.flatMap { stringList =>
val intList = stringList.map{ _.toInt }
refineV[AllPositiveInts](intList)
}
validatedIntList
}
parse("1,2,3,4,5,6")
Até a próxima, pessoal _o/"

Mais conteúdo relacionado

Destaque

Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...DevGAMM Conference
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationErica Santiago
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellSaba Software
 

Destaque (20)

Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
 

Refined Types

  • 1. Refined Types Tipagem ainda mais forte para Scala!
  • 2. Marcelo Gomes • Nasceu na pequena Juiz de Fora - MG • Morou 9 anos no Rio • Já mexeu com Delphi, Java, Ruby e Objective-C • Agora mora em São Paulo e trabalha com Scala
  • 3. Pra quê servem refined types? • Tipagem forte e estática é muito legal! (com inferência então…) • O compilador ajuda a evitar um monte de erros bobos • Mas na maior parte do tempo, isso não basta…
  • 6. Linha • Três formatos • 7600-10 • 209P-10 • 6L04-41
  • 7. Empresa • Pelo menos 3 caracteres • Não pode ser só espaços em branco
  • 8. case class SpBus( id: Int, line: String, company: String ) Parece OK, mas…
  • 9. …algo errado não está certo! val bus = SpBus( id = 0, line = "SV917", company = "" ) O compilador vai aceitar essa barbaridade!
  • 11. object SpBus { type IdRange = Interval.Closed[W.`10000`.T, W.`89999`.T] type LineRegex = MatchesRegex[ W.`"""^[1-8][0-9L][0-9][0-9A-Z]-d{2}$"""`.T] type MinSizeString = MinSize[W.`3`.T] And Not[MatchesRegex[W.`"""^s+$"""`.T]] } case class SpBus( id: Int Refined IdRange, line: String Refined LineRegex, company: String Refined MinSizeString )
  • 12. Agora, isso não compila mais! import eu.timepit.refined.auto._ val bus = SpBus( id = 0, line = "SV917", company = “” )
  • 13. Para valores não hardcoded, age como validação! def buildBus( id: Int, line: String, company: String): Either[String,SpBus] = { for { id <- refineV[IdRange](id) line <- refineV[LineRegex](line) company <- refineV[MinSizeString](company) } yield SpBus(id, line, company) }
  • 14. Não se pode ter tudo na vida… import eu.timepit.refined.auto._ type NonEmptyPositiveList = NonEmpty And Forall[Positive] val x: List[Int] Refined NonEmptyPositiveList = List(1,2,3,4,5,6,7,8,9) “compile-time refinement only works with literals”
  • 15. Mas não perca a esperança! import eu.timepit.refined.auto._ type NonEmptyValidInts = NonEmpty And Forall[ValidInt] type AllPositiveInts = Forall[Positive] type PositiveIntList = List[Int] Refined AllPositiveInts def parse(s: String): Either[String,PositiveIntList] = { val validatedStringList = refineV[NonEmptyValidInts](s.split(",").toList) val validatedIntList = validatedStringList .flatMap { stringList => val intList = stringList.map{ _.toInt } refineV[AllPositiveInts](intList) } validatedIntList } parse("1,2,3,4,5,6")
  • 16. Até a próxima, pessoal _o/"