Groovy and Scala
Friends or Foes?
Marco Vermeulen
Here be Dragons...
...and where you have dragons,
you have flames!
time to start building bridges!
About the Talk
- Type Systems
- Dynamically Typed Languages
- Statically Typed Languages
- Groovy and Scala
- Scala Features
- Demo
- Q&A / Discussion
Links
Strongly Typed Languages
“whenever an object is passed from a calling function to a called function, its type must be compatible with the type declared in the called function.”
~Liskov and ZillesType Checking Concepts
Strength
Type Checking Concepts
Dynamism
Statically Typed Languages
Declared values bound to a type and optionally an object.
Java
BigDecimal balance = new BigDecimal("39.42");
Scala
val balance: BigDecimal = 39.42
Dynamically Typed Languages
Declared values have no type until interpreted at runtime.
Python
message = 12345
print(message)
message = 'And now for something completely different.'
print(message)
Javascript
var total = 5;
total = 'x';
Dynamic Languages
“Dynamic programming language is a term used in computer science to describe a class of high-level programming languages which, at runtime, execute many common programming behaviors that static programming languages perform during compilation.”
~Wikipedia
Dynamic Languages
Pros
- Usually more concise
- No upfront compilation
- Faster turnaround
- Monkey patching
- Duck typing
- Stronger focus on TDD
- Inspires pragmatism
Dynamic Languages
Cons
- Runtime bugs
- Less performant
- Refactoring difficult
Statically Typed Languages
Statically Typed Languages
Pros
- Earlier detection of type errors
- Self documenting code
- Compiler optimisations
- Improved runtime efficiency
- Improved IDE support
Statically Typed Languages
Cons
- More verbose
- Slower development process
- Dilutes TDD
Groovy and Scala
where do they fit in?
Groovy
- JVM Platform Language
- Object Oriented
- Imperative
- also Functional?
- Optionally Typed
- Scripting
- Java interoperability
- Compiled, not interpreted
- Runtime Metaprogramming
- Can compile statically
- Groovy Console
- A swiss army knife for any application!
Scala
- JVM Platform Language
- Functional
- Object Oriented
- Strong Static Type System
- Behaves like dynamic language
- Terse like dynamic language
- Compiled
- REPL
- Cool language features!
Scala
some drawbacks
- slow compilation: type inference and implicits
- endless scrutenisation over code
- horrendous tooling support in IDEA
- highly functional code can be difficult to Unit Test
- very difficult to Debug in an IDE
-
SBT. Worst. Build. Tool. Ever.
Scala Language Features
Case Classes
scala> case class Person(id: Long, firstName: String, lastName: String)
scala> val p = Person(1, "John", "Appleseed")
scala> println(p)
Person(1,John,Appleseed)
Complete pojo in 1 line!
Constructor part of class declaration
Immutable fields
new keyword is obsolete
Generates getters, equals, hashcode and toString methods
Scala Language Features
Type Inference
//Java
ResponseEntity<SuccessResponse> response =
new ResponseEntity<>(new SuccessResponse(m.toString()))
//Scala
val response = ResponseEntity(SuccessResponse(m.toString))
left side of assignment infers type of right side
Scala Language Features
Implicit Conversion
scala> implicit def intToString(x: Int) = x.toString
scala> val y = 1 + "y"
y: String = 1y
types can be converted implicitly
Scala Language Features
Implicit Paramaters
scala> def whosit(x: String)(implicit y: String) = s"$x's your $y"
scala> implicit val y = "Uncle"
scala> println(whosit("Bob"))
Bob's your Uncle
implicit parameters need not be provided explicitly
string interpolation
concise method body
Scala Language Features
Options and Pattern Matchers
val person: Option[String] = ???
def message(p: Option[String]) = p match {
case Some("John") => "John has won!"
case Some(y) => s"$y wins"
case None => "Nobody is the winner :-("
}
message(person)
Optional types baked into the language
Pattern matching
Scala Language Features
-
for comprehensions
-
apply() and unapply() methods
-
Futures for Reactive programming
- Rich type system
- No statics, just Singletons
- Functional collections API: map(), flatMap(), filter() etc.
- cool frameworks: Akka, Play, Lift
- pretty neat testing with ScalaTest
- and many many more...
Demo!
Write some code for a contiguous range of numbers:
- the numbers
- 'fizz' for numbers that are multiples of 3
- 'buzz' for numbers that are multiples of 5
- 'fizzbuzz' for numbers that are multiples of 15
Runing for range 1-20 renders the following output:
1 2 fizz 4 buzz fizz 7 8 fizz buzz 11 fizz 13 14 fizzbuzz 16 17 fizz 19 buzz
Summary
- Compliments Groovy, does not Replace it
- Scala is an Allie, not an Enemy
- Academic community, but doesn't bite!
- Improves your Groovy code
- Be a Peacemaker, not a War Monger
- Have a lot of fun!
Groovy and Scala
Friends or Foes?
Marco Vermeulen