SlideShare a Scribd company logo
1 of 36
10 Things I Hate About Scala
About Me 
Meir Maor 
Chief Architect @ SparkBeyond 
At SparkBeyond we leverage the collective 
human knowledge to solve the world's toughest 
problems
1. Type Inference Limitations 
Pop Quiz: What does the following expression 
return? 
List("foo").toSet.head.head 
scala> List("foo").toSet.head.head 
<console>:8: error: value head is not a member of type parameter B 
List("foo").toSet.head.head 
^
Why? 
def toSet[B >: A]: immutable.Set[B] = 
to[immutable.Set].asInstanceOf[immutable.Set[B]] 
The required type should affect type inference: 
val a: Set[Any] = List("foo").toSet 
But not break Set Invariance: 
val aSet: Set[String] = Set("foo") 
val b: Set[Any] = aSet 
As we want to use [A] in both variant and co-variant positions: 
val s=Set("foo") 
List("foo","bar").filter(s)
Type Inference – Cont. 
Make up test: 
math.sqrt(Some(1).getOrElse(1)) 
<console>:8: error: type mismatch; 
found : AnyVal 
required: Double 
math.sqrt(Some(1).getOrElse(1)) 
^
Workarounds 
Use to[Set] instead of toSet: 
List("foo").to[Set].head.head 
Explicitly state type parameter [Int]: 
math.sqrt(Some(1).getOrElse[Int](1)) 
Assign a variable to force a concrete type sooner.
Fixing the root cause 
Requires adding Back-tracking after failure 
(less efficient) 
Not planned for upcoming release
2. Type Erasure 
List(“foo”) match { 
case x: List[Int] => 1 
case x: List[_] => 2 
}
Structural Type Erasure 
val z: Any = "foo" 
z match { 
case x: { def noSuchMethod() : Int } => "???" 
case _ => "OK" 
}
Type Erasure – Why 
Odersky did it. (in Java) 
Prevents generation of extra classes and 
overhead. 
Refied Types (like reflection) enable breaking 
type safety
Type Erasure - Workarounds 
def foo[T](a: T)(implicit ev: TypeTag[T]) = { 
ev.tpe <:< typeOf[Seq[String]] 
} 
def foo[T: TypeTag](a: T) = { 
typeOf[T] <:< typeOf[Seq[String]] 
} 
def putInArray[T: ClassTag](a: T) = { 
val res = new Array[T](1) 
res(0) = a 
res 
}
More Workarounds 
When TypeTags won't do: 
● Pattern match member of collection 
(implies non empty) 
● Check members reflectively explicitly 
(for structural type) 
● Restructure to keep type safety
Type Erasure - Solutions 
Fully Reified types - Not going to Happen 
More Sugar may be added around TypeTags 
while keeping them optional.
3) Binary Incompatibility 
● Scala 2.11.x / 2.10.x / 2.9.x aren’t compatible 
with each other. 
● Libraries need to be cross-compiled to different 
Scala versions. 
● Source compatibility isn’t perfect either.
Mitigations 
● Cross compiling, upgrade libraries, compile library 
from source yourself. 
● Community builds 
● Last resort: Separate JVM 
● Isolate ClassLoaders (OSGI)
4. Overloading with default arguments 
object Foo { 
def foo(a: Int, b: String = "foo") 
def foo(a: Double, b: String = "foo") 
} 
error: in object Foo, multiple 
overloaded alternatives of method 
foo define default arguments
Why? 
● In order to have a deterministic naming-scheme for the 
generated methods which return default arguments. 
If you write 
def foo(a: Int,b: String = "foo") 
the compiler generates: 
def foo$default$2 = "foo" 
● If two overloads have default parameters, 
the generated code will conflict.
Workaround 
Avoid overloads 
when you need 
default parameters :(
Fixing the root problem 
● Nothing stopping it. 
● Doesn’t seem to be anyone's top priority. 
● A possible solution would be 
adding the disambiguating types to the 
generated method signature.
5. Standard Library - Performance 
Dearth of overriding implementations for 
performance: 
def getOrElseUpdate(key: A, op: => B): B = 
get(key) match { 
case Some(v) => v 
case None => val d = op; this(key) = d; d 
}
override def add(elem: Int): Boolean = { 
require(elem >= 0) 
if (contains(elem)) false 
else { 
val idx = elem >> LogWL 
updateWord(idx, word(idx) | (1L << elem)) 
true 
} 
}
6. Standard Library - Quality 
Bugs escape: 
Range equality - 2.11.0, 2.11.1 
(0 until 0) == (0 until 10) // True 
(0 until 10) == (0 until 0) // NoSuchElementException 
Searching: (Should return Insertion point for missing value) 
Vector(1,2,4).search(0) // InsertionPoint(-1)
Why 
The Scala standard library is very rich 
Efforts focus more on the compiler
Mitigation 
Know what's going on inside 
Don't assume you can't do better than the 
standard library.
Solutions 
Focus of up-coming versions 
Smaller Standard Library
7. Equality type unsafety 
if (“foo” == 4) 1 else 2 
// Compiles no warning 
if (4 == “foo”) 1 else 2 
// Compiles with warning
Developers can do many things 
Proving the scala compiler wrong: 
case class Foo(a: Int) 
class Foo2(a: Int) extends Foo(a) { // Bad Idea 
override def equals(other: Any) = true 
} 
val a: Foo = Foo2(1) 
a == 1 
<console>:62: warning: comparing values of types Bar and 
Int using `==' will always yield false 
a == 1 
^ 
res39: Boolean = true
Why? 
● Interoperability with Java 
● Simplicity
Mitigations 
Scalaz === operator, gives compile time type safety. 
scala> 1 === 1 
res0: Boolean = true 
scala> 1 === "foo" 
<console>:14: error: could not find implicit value for parameter F0: 
scalaz.Equal[Object] 
1 === "foo" 
^
8. Explicit method return types 
There are (too) many cases you must define an explicit 
method return type: 
● Explicitly call return in a method (even at the end) 
● Recursive methods 
● A method is overloaded and one of the methods calls 
another. The calling method needs a return type 
annotation. 
● The inferred return type would be more general than 
you intended, e.g. Any
Why 
Some cases are difficult to infer 
Need simple rules for requiring explicit types 
Simple rules are overly broad
9. Compile time 
● Scala compilation lines/sec is 
~10X slower than Java 
● More functionality per line can shrink the 
difference to 3X-5X range (benchmarks vary wildly)
Solution / Mitigations: 
● SBT - for incremental compile (new flag) 
● Supply explicit return types 
● An area of continuous improvement in Scala
10. REPL Is Different 
val a = (1, 2) 
println(a.getClass) 
println(a.getClass.getSuperclass) 
Compiling and running normally: 
class scala.Tuple2$mcII$sp ←Specialization 
class scala.Tuple2 
From Repl: 
a: (Int, Int) = (1,2) 
class scala.Tuple2 
class java.lang.Object
Final Thoughts 
Scala is definitely production grade 
Happily been using in production for 3 years 
Undergoing constant improvement 
You can take an active part in improving Scala
Thank You 
Join Us

More Related Content

What's hot

From Ruby to Scala
From Ruby to ScalaFrom Ruby to Scala
From Ruby to Scalatod esking
 
Scala : language of the future
Scala : language of the futureScala : language of the future
Scala : language of the futureAnsviaLab
 
Solid and Sustainable Development in Scala
Solid and Sustainable Development in ScalaSolid and Sustainable Development in Scala
Solid and Sustainable Development in Scalascalaconfjp
 
Few simple-type-tricks in scala
Few simple-type-tricks in scalaFew simple-type-tricks in scala
Few simple-type-tricks in scalaRuslan Shevchenko
 
SE 20016 - programming languages landscape.
SE 20016 - programming languages landscape.SE 20016 - programming languages landscape.
SE 20016 - programming languages landscape.Ruslan Shevchenko
 
Android course session 5 (Threads & socket)
Android course session 5 (Threads & socket)Android course session 5 (Threads & socket)
Android course session 5 (Threads & socket)Keroles M.Yakoub
 
Scala: functional programming for the imperative mind
Scala: functional programming for the imperative mindScala: functional programming for the imperative mind
Scala: functional programming for the imperative mindSander Mak (@Sander_Mak)
 
A Brief, but Dense, Intro to Scala
A Brief, but Dense, Intro to ScalaA Brief, but Dense, Intro to Scala
A Brief, but Dense, Intro to ScalaDerek Chen-Becker
 
Scala the-good-parts
Scala the-good-partsScala the-good-parts
Scala the-good-partsFuqiang Wang
 
002. Introducere in type script
002. Introducere in type script002. Introducere in type script
002. Introducere in type scriptDmitrii Stoian
 
An Introduction to Scala for Java Developers
An Introduction to Scala for Java DevelopersAn Introduction to Scala for Java Developers
An Introduction to Scala for Java DevelopersMiles Sabin
 
Java Intro
Java IntroJava Intro
Java Introbackdoor
 
Speaking Scala: Refactoring for Fun and Profit (Workshop)
Speaking Scala: Refactoring for Fun and Profit (Workshop)Speaking Scala: Refactoring for Fun and Profit (Workshop)
Speaking Scala: Refactoring for Fun and Profit (Workshop)Tomer Gabel
 
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...Edureka!
 
Miles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java DevelopersMiles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java DevelopersSkills Matter
 

What's hot (18)

From Ruby to Scala
From Ruby to ScalaFrom Ruby to Scala
From Ruby to Scala
 
Scala : language of the future
Scala : language of the futureScala : language of the future
Scala : language of the future
 
Scala - core features
Scala - core featuresScala - core features
Scala - core features
 
Solid and Sustainable Development in Scala
Solid and Sustainable Development in ScalaSolid and Sustainable Development in Scala
Solid and Sustainable Development in Scala
 
Few simple-type-tricks in scala
Few simple-type-tricks in scalaFew simple-type-tricks in scala
Few simple-type-tricks in scala
 
SE 20016 - programming languages landscape.
SE 20016 - programming languages landscape.SE 20016 - programming languages landscape.
SE 20016 - programming languages landscape.
 
Android course session 5 (Threads & socket)
Android course session 5 (Threads & socket)Android course session 5 (Threads & socket)
Android course session 5 (Threads & socket)
 
Scala: functional programming for the imperative mind
Scala: functional programming for the imperative mindScala: functional programming for the imperative mind
Scala: functional programming for the imperative mind
 
A Brief, but Dense, Intro to Scala
A Brief, but Dense, Intro to ScalaA Brief, but Dense, Intro to Scala
A Brief, but Dense, Intro to Scala
 
Scala the-good-parts
Scala the-good-partsScala the-good-parts
Scala the-good-parts
 
Scala jargon cheatsheet
Scala jargon cheatsheetScala jargon cheatsheet
Scala jargon cheatsheet
 
002. Introducere in type script
002. Introducere in type script002. Introducere in type script
002. Introducere in type script
 
camel-scala.pdf
camel-scala.pdfcamel-scala.pdf
camel-scala.pdf
 
An Introduction to Scala for Java Developers
An Introduction to Scala for Java DevelopersAn Introduction to Scala for Java Developers
An Introduction to Scala for Java Developers
 
Java Intro
Java IntroJava Intro
Java Intro
 
Speaking Scala: Refactoring for Fun and Profit (Workshop)
Speaking Scala: Refactoring for Fun and Profit (Workshop)Speaking Scala: Refactoring for Fun and Profit (Workshop)
Speaking Scala: Refactoring for Fun and Profit (Workshop)
 
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...
 
Miles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java DevelopersMiles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java Developers
 

Similar to 10 Things I Hate About Scala

Static or Dynamic Typing? Why not both?
Static or Dynamic Typing? Why not both?Static or Dynamic Typing? Why not both?
Static or Dynamic Typing? Why not both?Mario Camou Riveroll
 
Scala: Object-Oriented Meets Functional, by Iulian Dragos
Scala: Object-Oriented Meets Functional, by Iulian DragosScala: Object-Oriented Meets Functional, by Iulian Dragos
Scala: Object-Oriented Meets Functional, by Iulian Dragos3Pillar Global
 
Introduction to clojure
Introduction to clojureIntroduction to clojure
Introduction to clojureAbbas Raza
 
Effective Scala (JavaDay Riga 2013)
Effective Scala (JavaDay Riga 2013)Effective Scala (JavaDay Riga 2013)
Effective Scala (JavaDay Riga 2013)mircodotta
 
How Scala promotes TDD
How Scala promotes TDDHow Scala promotes TDD
How Scala promotes TDDShai Yallin
 
(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?Tomasz Wrobel
 
Clojure - A new Lisp
Clojure - A new LispClojure - A new Lisp
Clojure - A new Lispelliando dias
 
What’s new in .NET
What’s new in .NETWhat’s new in .NET
What’s new in .NETDoommaker
 
Introduction à Scala - Michel Schinz - January 2010
Introduction à Scala - Michel Schinz - January 2010Introduction à Scala - Michel Schinz - January 2010
Introduction à Scala - Michel Schinz - January 2010JUG Lausanne
 
Spring Day | Spring and Scala | Eberhard Wolff
Spring Day | Spring and Scala | Eberhard WolffSpring Day | Spring and Scala | Eberhard Wolff
Spring Day | Spring and Scala | Eberhard WolffJAX London
 
A brief tour of modern Java
A brief tour of modern JavaA brief tour of modern Java
A brief tour of modern JavaSina Madani
 
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
 
Java Tutorial | My Heart
Java Tutorial | My HeartJava Tutorial | My Heart
Java Tutorial | My HeartBui Kiet
 

Similar to 10 Things I Hate About Scala (20)

Scala ntnu
Scala ntnuScala ntnu
Scala ntnu
 
Static or Dynamic Typing? Why not both?
Static or Dynamic Typing? Why not both?Static or Dynamic Typing? Why not both?
Static or Dynamic Typing? Why not both?
 
Scala: Object-Oriented Meets Functional, by Iulian Dragos
Scala: Object-Oriented Meets Functional, by Iulian DragosScala: Object-Oriented Meets Functional, by Iulian Dragos
Scala: Object-Oriented Meets Functional, by Iulian Dragos
 
Introduction to clojure
Introduction to clojureIntroduction to clojure
Introduction to clojure
 
Effective Scala (JavaDay Riga 2013)
Effective Scala (JavaDay Riga 2013)Effective Scala (JavaDay Riga 2013)
Effective Scala (JavaDay Riga 2013)
 
Scala in a nutshell by venkat
Scala in a nutshell by venkatScala in a nutshell by venkat
Scala in a nutshell by venkat
 
How Scala promotes TDD
How Scala promotes TDDHow Scala promotes TDD
How Scala promotes TDD
 
Redis Lua Scripts
Redis Lua ScriptsRedis Lua Scripts
Redis Lua Scripts
 
(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?
 
Clojure - A new Lisp
Clojure - A new LispClojure - A new Lisp
Clojure - A new Lisp
 
What’s new in .NET
What’s new in .NETWhat’s new in .NET
What’s new in .NET
 
Introduction à Scala - Michel Schinz - January 2010
Introduction à Scala - Michel Schinz - January 2010Introduction à Scala - Michel Schinz - January 2010
Introduction à Scala - Michel Schinz - January 2010
 
Spring Day | Spring and Scala | Eberhard Wolff
Spring Day | Spring and Scala | Eberhard WolffSpring Day | Spring and Scala | Eberhard Wolff
Spring Day | Spring and Scala | Eberhard Wolff
 
A brief tour of modern Java
A brief tour of modern JavaA brief tour of modern Java
A brief tour of modern Java
 
Java tutorials
Java tutorialsJava tutorials
Java tutorials
 
Dallas Scala Meetup
Dallas Scala MeetupDallas Scala Meetup
Dallas Scala Meetup
 
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)
 
scala.ppt
scala.pptscala.ppt
scala.ppt
 
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
 
Java Tutorial | My Heart
Java Tutorial | My HeartJava Tutorial | My Heart
Java Tutorial | My Heart
 

Recently uploaded

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
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
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
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
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
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
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
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
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
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 

Recently uploaded (20)

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
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
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
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
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...
 
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
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
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
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
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...
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
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
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 

10 Things I Hate About Scala

  • 1. 10 Things I Hate About Scala
  • 2. About Me Meir Maor Chief Architect @ SparkBeyond At SparkBeyond we leverage the collective human knowledge to solve the world's toughest problems
  • 3. 1. Type Inference Limitations Pop Quiz: What does the following expression return? List("foo").toSet.head.head scala> List("foo").toSet.head.head <console>:8: error: value head is not a member of type parameter B List("foo").toSet.head.head ^
  • 4. Why? def toSet[B >: A]: immutable.Set[B] = to[immutable.Set].asInstanceOf[immutable.Set[B]] The required type should affect type inference: val a: Set[Any] = List("foo").toSet But not break Set Invariance: val aSet: Set[String] = Set("foo") val b: Set[Any] = aSet As we want to use [A] in both variant and co-variant positions: val s=Set("foo") List("foo","bar").filter(s)
  • 5. Type Inference – Cont. Make up test: math.sqrt(Some(1).getOrElse(1)) <console>:8: error: type mismatch; found : AnyVal required: Double math.sqrt(Some(1).getOrElse(1)) ^
  • 6. Workarounds Use to[Set] instead of toSet: List("foo").to[Set].head.head Explicitly state type parameter [Int]: math.sqrt(Some(1).getOrElse[Int](1)) Assign a variable to force a concrete type sooner.
  • 7. Fixing the root cause Requires adding Back-tracking after failure (less efficient) Not planned for upcoming release
  • 8. 2. Type Erasure List(“foo”) match { case x: List[Int] => 1 case x: List[_] => 2 }
  • 9. Structural Type Erasure val z: Any = "foo" z match { case x: { def noSuchMethod() : Int } => "???" case _ => "OK" }
  • 10. Type Erasure – Why Odersky did it. (in Java) Prevents generation of extra classes and overhead. Refied Types (like reflection) enable breaking type safety
  • 11. Type Erasure - Workarounds def foo[T](a: T)(implicit ev: TypeTag[T]) = { ev.tpe <:< typeOf[Seq[String]] } def foo[T: TypeTag](a: T) = { typeOf[T] <:< typeOf[Seq[String]] } def putInArray[T: ClassTag](a: T) = { val res = new Array[T](1) res(0) = a res }
  • 12. More Workarounds When TypeTags won't do: ● Pattern match member of collection (implies non empty) ● Check members reflectively explicitly (for structural type) ● Restructure to keep type safety
  • 13. Type Erasure - Solutions Fully Reified types - Not going to Happen More Sugar may be added around TypeTags while keeping them optional.
  • 14. 3) Binary Incompatibility ● Scala 2.11.x / 2.10.x / 2.9.x aren’t compatible with each other. ● Libraries need to be cross-compiled to different Scala versions. ● Source compatibility isn’t perfect either.
  • 15. Mitigations ● Cross compiling, upgrade libraries, compile library from source yourself. ● Community builds ● Last resort: Separate JVM ● Isolate ClassLoaders (OSGI)
  • 16. 4. Overloading with default arguments object Foo { def foo(a: Int, b: String = "foo") def foo(a: Double, b: String = "foo") } error: in object Foo, multiple overloaded alternatives of method foo define default arguments
  • 17. Why? ● In order to have a deterministic naming-scheme for the generated methods which return default arguments. If you write def foo(a: Int,b: String = "foo") the compiler generates: def foo$default$2 = "foo" ● If two overloads have default parameters, the generated code will conflict.
  • 18. Workaround Avoid overloads when you need default parameters :(
  • 19. Fixing the root problem ● Nothing stopping it. ● Doesn’t seem to be anyone's top priority. ● A possible solution would be adding the disambiguating types to the generated method signature.
  • 20. 5. Standard Library - Performance Dearth of overriding implementations for performance: def getOrElseUpdate(key: A, op: => B): B = get(key) match { case Some(v) => v case None => val d = op; this(key) = d; d }
  • 21. override def add(elem: Int): Boolean = { require(elem >= 0) if (contains(elem)) false else { val idx = elem >> LogWL updateWord(idx, word(idx) | (1L << elem)) true } }
  • 22. 6. Standard Library - Quality Bugs escape: Range equality - 2.11.0, 2.11.1 (0 until 0) == (0 until 10) // True (0 until 10) == (0 until 0) // NoSuchElementException Searching: (Should return Insertion point for missing value) Vector(1,2,4).search(0) // InsertionPoint(-1)
  • 23. Why The Scala standard library is very rich Efforts focus more on the compiler
  • 24. Mitigation Know what's going on inside Don't assume you can't do better than the standard library.
  • 25. Solutions Focus of up-coming versions Smaller Standard Library
  • 26. 7. Equality type unsafety if (“foo” == 4) 1 else 2 // Compiles no warning if (4 == “foo”) 1 else 2 // Compiles with warning
  • 27. Developers can do many things Proving the scala compiler wrong: case class Foo(a: Int) class Foo2(a: Int) extends Foo(a) { // Bad Idea override def equals(other: Any) = true } val a: Foo = Foo2(1) a == 1 <console>:62: warning: comparing values of types Bar and Int using `==' will always yield false a == 1 ^ res39: Boolean = true
  • 28. Why? ● Interoperability with Java ● Simplicity
  • 29. Mitigations Scalaz === operator, gives compile time type safety. scala> 1 === 1 res0: Boolean = true scala> 1 === "foo" <console>:14: error: could not find implicit value for parameter F0: scalaz.Equal[Object] 1 === "foo" ^
  • 30. 8. Explicit method return types There are (too) many cases you must define an explicit method return type: ● Explicitly call return in a method (even at the end) ● Recursive methods ● A method is overloaded and one of the methods calls another. The calling method needs a return type annotation. ● The inferred return type would be more general than you intended, e.g. Any
  • 31. Why Some cases are difficult to infer Need simple rules for requiring explicit types Simple rules are overly broad
  • 32. 9. Compile time ● Scala compilation lines/sec is ~10X slower than Java ● More functionality per line can shrink the difference to 3X-5X range (benchmarks vary wildly)
  • 33. Solution / Mitigations: ● SBT - for incremental compile (new flag) ● Supply explicit return types ● An area of continuous improvement in Scala
  • 34. 10. REPL Is Different val a = (1, 2) println(a.getClass) println(a.getClass.getSuperclass) Compiling and running normally: class scala.Tuple2$mcII$sp ←Specialization class scala.Tuple2 From Repl: a: (Int, Int) = (1,2) class scala.Tuple2 class java.lang.Object
  • 35. Final Thoughts Scala is definitely production grade Happily been using in production for 3 years Undergoing constant improvement You can take an active part in improving Scala