Programming in Scala Gist 5

Vinu Charanya
vTechNotes
Published in
6 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 5 — Basic Type and Operations

5.1 Some basic types

  • Collectively, types, Byte, Short, Int, Long and Char are called integral types. The integral types plus Float and Double are called numeric types.
  • String resides in java.lang package and all other types are members of Scala package.
  • Scala compiler can transform scala types to Java primitive types in the bytecodes it produces

5.2 Literals

  • A literal is a shorthand way to describe an object, where the shorthand exactly mirrors the structure of the created object. A literal is a way to write a constant value directly in code.
  • Integer literals 3 forms: Decimal (regular), octal (starts with 0) and hexadecimal (starts with 0x)
  • Scala shell always prints integer values in base 10.
  • An Int is treated as Short or byte if assigned as long as it is within range.
  • Floating point literals has decimal points and are optionally followed by an E or e and an exponent
  • Character Literals are composed by Unicode character in single quotes.
  • A character literal can be provided as an octal ‘\0 or hex number ‘\u0041’
  • String Literal is composed of characters within double quotes
  • Special characters within strings like “ or \ can be escaped with a backslash
  • “””(three double quotes) is a special syntax for raw strings within which you don’t have to escape anything with backslash.
  • To strip space in the front of a line add a (|) and call stripMargin on the whole string.
  • Boolean Literal has two literals true and false

Symbol Literals

  • A symbol is similar to ruby symbol. In Scala it is represented with a single quote just at the front. They are mapped to instances of the predefined class scala.Symbol
  • Symbol saves memory as they are interned. Using the same symbol multiple times, will refer to the same Symbol object

5.3 Operators are methods

  • Operators are nicer syntax for method calls. Any method can be invoked in operator notation

val a = 1 + 2 //Scala invokes (1).+(2)

val s = “Hello, World!”

s indexOf ‘o’ //Scala invokes s.indexOf(‘o’)

  • Scala has 3 operator notations prefix, infix and postfix based on where the operator method’s position is.
  • Above examples are infix. Prefix: +7, -6 Postfix: 6 toLong , 5 toString. Prefix and Postfix operators are unary.
  • Prefix operators are a shorthand for actual methods which are defined with unary_ . Only identifiers that can be used as prefix operators are +,-,! and ~. You cannot create custom prefix methods.

scala> -2.0 // Scala invokes (2.0).unary_-

  • Postfix operators are methods that do not take arguments when invoked without a dot or parenthesis
  • Empty parentheses can be dropped while invoking, however if a method has side effects, the convention is to add them like println()

5.4 Arithmetic Operators

  • Arithmetic methods are invoked via infix operator notation. Addition (+), Subtraction (-), Multiplication (*), Division (/) and remainder (%) on any numeric type.
  • The floating point remainder is not the one defined by IEEE754 which uses rounding division not truncating division. To get this call IEEEremainder on scala.math

scala > math.IEEEremainder(11.0, 4.0) // res14: Double = -1.0

  • Unary + or — can be used to indicate a literal number or variable holding a literal as positive or negative

scala> var neg = -2 // neg: Int = -2

scala> -neg // res0: Int = 2

5.5 Relational and logical operations

  • Compare numeric types with relational methods >, < , >=, <=, !
  • Logical methods &&, || yield a boolean result and are short circuited(evaluated only as far as needed)

5.6 Bitwise Operations

  • Bitwise scala methods are bitwise-and(&), bitwise-or(|) and bitwise-xor(^) and bitwise complement operator(~)
  • Scala shift methods: shift left (<<), shift right (>>) , unsigned shift right (>>>)
  • The leftmost bit in an integer type is the sign bit. If the leftmost bit is 1, the number is negative. If 0, the number is positive.
  • The shift methods, when used in infix operator notation, shift the integer value on the left of the operator by the amount specified by the integer value on the right. Shift left and unsigned shift right fill with zeroes as they shift. Shift right fills with the highest bit (the sign bit) of the left-hand value as it shifts.

5.7 Object Equality

  • To compare two objects for equality, use == or !=

5.8 Operator Precedence and Associativity

  • Operator precedence determines which parts of an expression are evaluated before other parts.
  • Associativity of the operators determine the way operators are grouped when multiple operators have the same precedence
  • Operands are always evaluated from left to right.

{ val x = a; b.:::(x) } // a is evaluated first, assigned to x and x is then passed to b

  • This associativity rule also plays a role when multiple operators of the same precedence appear side by side.
  • If the methods end in ‘:’, they are grouped right to left; otherwise, they are grouped left to right. For example, a ::: b ::: c is treated as a ::: (b ::: c). But a * b * c, by contrast, is treated as (a * b) * c

5.9 Rich Wrapper

  • Implicit conversion is a technique that can can be used to invoke more methods on Scala’s basic types.
  • Each basic type has a rich wrapper that provides additional methods.

Conclusion

  • Operators in scala are method calls.
  • Implicit conversion to rich variants exist for Scala’s basic type

--

--