Programming in Scala Gist 2

Vinu Charanya
vTechNotes
Published in
3 min readMay 25, 2016

These are the key points from the Programming in Scala, Second Edition by Martin Odersky, Lex Spoon, Bill Venners. I wanted to make note of some key points. They may seem broken and not make much sense, if you have not read the book. This is for personal reference. Feel free to correct me if I have interpreted anything wrong in this post.

Chapter 2 — First Steps in Scala

1. Learn to use the Scala interpreter

  • Try some expressions in Scala command line.
  • Packages in Scala are similar to packages in Java. They partition global namespace and provide a mechanism for information hiding.
  • All of java’s primitive types have corresponding classes in the Scala package.

2. Define some variables

  • Two kind of variables — vals and vars
  • A val is similar to a final variable in Java — cannot be reassigned.
  • A var by contrast is similar to a non-final variable that can be reassigned.
  • Scala has type inference.
  • An explicit type annotation can both ensure the Scala compiler infers the type you intend, as well as useful documentation.
  • In Scala you specify a variable’s type after its name, separated by a colon.

3. Define some functions

  • Function definitions start with def.
  • The equal sign that precedes the body of a function hints in the functional world view, a function defines an expression that results in a value.
  • Scala result type may or may not be explicitly mentioned based on whether the result can be inferred.
  • If a function consists of one statement, optionally leave off the curly braces.
  • A function that takes no parameters and returns no interesting result:

scala> def greet() = println(“Hello, world!”)greet: ()Unit

  • A result type of Unit indicates the function returns no interesting value.

4. Write some Scala scripts

  • Run scala script using scala filename.scala
  • Command line arguments to a Scala script are available via a Scala array named args that are zero based.
  • // and /**/ are used for scala comments

5. Loop with while; decide with if

  • block — code within curly braces
  • 2 spaces are recommended indentation style for Scala
  • boolean expression for while and if needs to be within the parenthesis.

6. Iterate with foreach and for

  • Scala enables you to program imperatively — you give one command at a time, iterate with loops and often mutate state shared between different functions.

args.foreach(arg => println(arg))

args.foreach((arg: String) => println(arg))

args.foreach(println)

  • 2. is more explicit way to pass argument with type. 3. is the most concise way
  • If a function literal consists of one statement that takes a single argument, you need not explicitly name and specify the argument.
  • The syntax of a function literal is a list of named parameters in parenthesis, a right arrow, and then the body of the function.

Conclusion

This chapter covers some super basic Scala to get one started.

--

--