Laugh, Then Learn:
Returns!
Andrew Phillips & Nermin Serifovic
Use left and right arrows for navigation.
Andrew Phillips & Nermin Serifovic
Use left and right arrows for navigation.
Slides prepared using replhtml from @retronym: https://gist.github.com/retronym/5520762
scalapuzzlers.com/buy-the-book
Scala Days discount:
2
2
2
2
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!"
1. Prints:4
|
2. Prints:4
|
3. Prints:4
|
4. Prints:4
|
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
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)
|
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.
1. Prints:
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)
|
4. The first one prints:List(1, 2, 3) and the second one fails to compile |
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.
1. First, second & third print:
Keep looking.
and the forth throws a runtime exception
|
2. First & second print:
Keep looking.
and third & forth throw a runtime exception
|
3. All four throw a runtime exception. |
4. Prints:
Keep looking.
|
Answer #1:
First, second & third print:
Keep looking.
and the forth throws a runtime exception
Nothing here.
Keep looking.
The value provided for withDefaultValue
is shared across all map entries.
3
4
6
Answer #1:
Prints 3
return
statement returns from enclosing def
and function values do not qualify as an enclosing function.
Set(1, 2, 3)
false3
123
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.
1. Prints:
2
|
2. Prints:
1
|
3. Prints:
4
|
4. Prints:
3
|
Answer #3:
Prints:
4
3
As Josh Suereth summarizes:
Names are static; values are runtime.
/