Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Scala Design Patterns

You're reading from   Scala Design Patterns Write efficient, clean, and reusable code with Scala

Arrow left icon
Product type Paperback
Published in Feb 2016
Publisher
ISBN-13 9781785882500
Length 382 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Ivan Nikolov Ivan Nikolov
Author Profile Icon Ivan Nikolov
Ivan Nikolov
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. The Design Patterns Out There and Setting Up Your Environment FREE CHAPTER 2. Traits and Mixin Compositions 3. Unification 4. Abstract and Self Types 5. Aspect-Oriented Programming and Components 6. Creational Design Patterns 7. Structural Design Patterns 8. Behavioral Design Patterns – Part 1 9. Behavioral Design Patterns – Part 2 10. Functional Design Patterns – The Deep Theory 11. Functional Design Patterns – Applying What We Learned 12. Real-Life Applications Index

Functions and classes

In Scala, every value is an object. Functions are first class values, which also makes them objects of their respective classes. The following figure shows the Scala unified type system and how this is achieved. It is adapted from http://www.scala-lang.org/old/sites/default/files/images/classhierarchy.png and represents an up-to-date view of the model (some classes such as ScalaObject have disappeared, as we have already mentioned before).

As you can see, Scala does not have the same concept of primitive types that Java has and all types are ultimately subtypes of Any.

Functions and classes

Functions as classes

The fact that functions are classes means that they can be freely passed to other methods or classes as if they were just values. This leads to improving the expressiveness of Scala and making it much easier to achieve things, such as callbacks, than in other languages such as Java.

Function literals

Let's have a look at an example:

class FunctionLiterals { 
  val sum = (a: Int,...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image