Scala_Chapter_1
Download
Report
Transcript Scala_Chapter_1
Programming in Scala
Chapter 1
Scala: both object-oriented and functional
• Scala blends
– object-oriented and
– functional programming in a
– statically typed language.
Object-oriented programming
• All but the most trivial programs need some sort
of structure.
• Put data and operations into some form of
containers.
• Make these containers fully general, so that they
can contain operations as well as data
• They are themselves are also values that can be
stored in other containers, or passed as
parameters to operations.
• Such containers are called objects.
Object-oriented programming (cont.)
• Many languages admit values that are not objects, such
as the primitive values in Java.
• Or they allow static fields and methods that are not
members of any object.
• These deviations from the pure idea of object-oriented
programming look quite harmless at first, but they have
an annoying tendency to complicate things and limit
scalability.
• Scala is an object-oriented language in pure form.
– Every value is an object.
– Every operation is a method call.
– For example, when you say 1 + 2 in Scala, you are actually
invoking a method named + defined in class Int. Like
new Integer(1).plus(new Integer(2))
Functional programming:
two big ideas
• Functions are first-class values—with the same
status as an integer or a string.
– You can pass functions as arguments, return them as
results, or store them in variables.
– You can define functions without giving them a name.
• No side-effects. Inputs are mapped to outputs,
but no data is changed. Instead new values are
generated. Like Strings in Java, values are
immutable.
• Scala encourages functional programming but
doesn’t require it.
Why Scala?
rather than Haskell or OCaml
or Ruby, Python, or Grooby
• Compatible with Java. Compiles to the JVM.
– But so does JRuby and Groovy.
• Interoperable with Java code. Scala code can
–
–
–
–
–
–
–
–
call Java methods,
access Java fields,
inherit from Java classes,
implement Java interfaces.
Scala makes heavy behind the scenes use of Java libraries.
Extends some, e.g. String. How?
String s = “123”; s.toInt(); -- How is this possible?
Declared implicit conversion.
• Since toInt() is not defined in String, convert to RichString.
Scala is concise
Typical savings of 50%.
// this is Java
class MyClass {
private int index;
private String name;
public MyClass(int index, String name) {
this.index = index;
this.name = name;
}
}
new myClass(3, “abc”)
// this is Scala
class MyClass(index: Int, name: String)
new myClass(3, “abc”)
// Instance variables and constructor defined implicitly
Scala is high level
// this is Java
boolean nameHasUpperCase = false;
for (int i = 0; i < name.length(); ++i) {
if (Character.isUpperCase(name.charAt(i))) {
nameHasUpperCase = true;
break;
}
}
// this is Scala
val nameHasUpperCase = name.exists(_.isUpperCase)
// exists() causes isUpperCase() to be applied to each
// element in name until one that satisfies it is found.
// Note that isUpperCase() is a function being passed as
// an argument to exists(). Could do something similar
// in Java but would have to build a class. Most people find
// it too much trouble to bother.
Scala is statically typed
• Strong typing is very important in
functional programming
– Most scripting like Ruby, Python, etc. are
dynamically typed, which is supposed to be one
of their advantages.
– Static typing makes programming much safer.
– But sometimes types declarations become a
burden.
– Scala’s type inferencing mechanism minimizes
the number of type declarations required.
• It’s not uncommon for user code to have no explicit
types.