Laugh, Then Learn:

Returns!

Andrew Phillips & Nermin Serifovic

@ScalaPuzzlers

Use left and right arrows for navigation.

First Things First

Thanks!

Slides prepared using replhtml from @retronym: https://gist.github.com/retronym/5520762

Shameless Plug

scalapuzzlers.com/buy-the-book

Scala Days discount:

scaladays2014

What is a Puzzler?

What is a Puzzler?

What is a Puzzler?

What is a Puzzler?

What is a Puzzler?

What is a Puzzler?

It'll be More Fun if...

Buckle Up

Stringy Conversions

Stringy Conversions

  1. Prints:
    2
    2
  2. The first statement prints: 2
    and the second throws a runtime exception
  3. The first statement fails to compile
    and the second prints: 2
  4. The first statement fails to compile
    and the second throws a runtime exception

Stringy Conversions

Answer #2:
The first statement prints: 2
and the second throws a runtime exception

Since type of stringToInt in B is known upfront,
it is selected to perform the conversion,
causing an infinite loop

Miles Sabin: "Don't do this!"

Size It Up

Size It Up

1. Prints:
4
5
5
2. Prints:
4
4
3
3. Prints:
4
4
4
4. Prints:
4
36
3

Size It Up

Answer #4:
4
36
3

+ is defined for Set, but not for Iterable

So, the second + represents String concatentation
and this applies because of the existence of the evil any2stringadd

One, Two, Skip a Few

One, Two, Skip a Few

1. Prints:
Permutations of 1 and 2:
List(1, 2)
List(2, 1)
Permutations of 3 and 4:
List(3, 4)
List(4, 3)
2. Prints:
Permutations of 1 and 2:
List(1, 2)
Permutations of 3 and 4:
List(3, 4)
3. Prints:
Permutations of 1 and 2:
List(1, 2)
List(2, 1)
4. Prints:
Permutations of 1 and 2:
Permutations of 3 and 4:
List(3, 4)
List(4, 3)

One, Two, Skip a Few

Answer #4:
Permutations of 1 and 2:
Permutations of 3 and 4:
List(3, 4)
List(4, 3)

permutations returns an Iterator
which is mutable

Never call any methods except next and hasNext on an iterator
after calling a method on it.

A Listful of Dollars

A Listful of Dollars

1. Prints:
List(1, 2, 3)
List(1, 2, 3)
2. The first one prints:
List(1, 1, 1)
and the second one throws
a runtime exception
3. Prints:
List(1, 1, 1)
List(1, 1, 1)
4. The first one prints:
List(1, 2, 3)
and the second one fails
to compile

A Listful of Dollars

Answer #2:
The first one prints: List(1, 1, 1)
and the second one throws a runtime exception

{ a: Int => Dollar } is parsed as:
{ (a: Int) => Dollar }

(a: Int => Dollar) is parsed as:
(a: (Int => Dollar)}

List is a Function1 and { ... } can enclose a ResultExpr,
whereas ( ... ) can only enclose an Expr. A ResultExpr, but not an Expr, can include a type specification for its argument.

The Devil Is in the Defaults

The Devil Is in the Defaults

1. First, second & third print:
Keep looking.
Nothing here.
Keep looking.
and the forth throws a runtime exception
2. First & second print:
Keep looking.
Nothing here.
and third & forth throw a runtime exception
3. All four throw a runtime exception. 4. Prints:
Keep looking.
Nothing here.
Keep looking.
Nothing here.

The Devil Is in the Defaults

Answer #1:
First, second & third print:
Keep looking.
Nothing here.
Keep looking.
and the forth throws a runtime exception

The value provided for withDefaultValue
is shared across all map entries.

Return to Me!

Return to Me!

  1. Prints: 3
  2. Prints: 4
  3. Prints: 6
  4. Fails with a compilation error: unreachable code

Return to Me!

Answer #1:
Prints 3

return statement returns from enclosing def
and function values do not qualify as an enclosing function.

Set the Record Straight

Set the Record Straight

  1. Prints: Set(1, 2, 3)
  2. Fails with a compilation error
  3. Prints: false3
  4. Prints: 123

Set the Record Straight

Answer #3:
Prints: false3

toSet method is parameterless

empty argument list is adapted to (): Unit
and is passed to the apply method on the resulting set,
which tests for inclusion of an item in the set.
This is likely to "go away" in future versions.

What's in a Name?

What's in a Name?

1. Prints:
2
3
2. Prints:
1
3
3. Prints:
4
3
4. Prints:
3
3

What's in a Name?

Answer #3:
Prints:
4
3

As Josh Suereth summarizes:
Names are static; values are runtime.

Thanks!

Any questions?

#

/