Skip to content

Commit 19cbfd8

Browse files
committed
Shorten oop again
1 parent 9facc07 commit 19cbfd8

11 files changed

+41
-46
lines changed
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/**
22
* Scala is object-oriented and it's always like...write less do more :)
33
*/
4-
class DumpClass
4+
class AClass
55

6-
val d = new DumpClass
6+
val d = new AClass
77

88
val isAny = d.isInstanceOf[Any]
99

10-
class AnotherDumpClass extends DumpClass
10+
class BClass extends AClass
1111

12-
val isDump = new AnotherDumpClass().isInstanceOf[DumpClass]
13-
val isAnotherDump = new AnotherDumpClass().isInstanceOf[AnotherDumpClass]
12+
val isA = new BClass().isInstanceOf[AClass]
13+
val isB = new BClass().isInstanceOf[BClass]

src/main/worksheets/c_oop/a_intro_2_fields_methods.sc

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ import scala.util.Random
33
/**
44
* Class with field and methods
55
*/
6-
class LunchDecision {
7-
// a private member
8-
private val choices: Seq[String] = Seq("Pizza", "Sushi", "Burger")
6+
class Lunch {
7+
private val where = Seq("Pizza", "Sushi", "Burger")
8+
9+
// Public API should have return type
10+
def decide(): String = where(Random.nextInt(where.size))
911

10-
// Type inference: remove return type
11-
// syntactic sugar: remove curly braces
12-
def decide() = choices(Random.nextInt(choices.size))
1312
}
1413

15-
val lunch: String = new LunchDecision().decide()
14+
val lunch = new Lunch().decide()
Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
1-
import scala.util.Random
2-
31
/**
42
* Scala is object-oriented and it's always like...write less do more :)
53
*/
6-
class DumpClass
4+
class AClass
75

8-
val d = new DumpClass
6+
val d = new AClass
97

108
val isAny = d.isInstanceOf[Any]
119

12-
class AnotherDumpClass extends DumpClass
10+
class BClass extends AClass
1311

14-
val isDump = new AnotherDumpClass().isInstanceOf[DumpClass]
15-
val isAnotherDump = new AnotherDumpClass().isInstanceOf[AnotherDumpClass]
12+
val isA = new BClass().isInstanceOf[AClass]
13+
val isB = new BClass().isInstanceOf[BClass]
14+
15+
import scala.util.Random
1616

1717
/**
1818
* Class with field and methods
1919
*/
20-
class LunchDecision {
21-
// a private member
22-
private val choices: Seq[String] = Seq("Pizza", "Sushi", "Burger")
20+
class Lunch {
21+
private val where = Seq("Pizza", "Sushi", "Burger")
22+
23+
// Public API should have return type
24+
def decide(): String = where(Random.nextInt(where.size))
2325

24-
// Type inference: remove return type
25-
// syntactic sugar: remove curly braces
26-
def decide() = choices(Random.nextInt(choices.size))
2726
}
2827

29-
val lunch: String = new LunchDecision().decide()
28+
val lunch = new Lunch().decide()

src/main/worksheets/c_oop/b_constructors_1_parameters.sc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import scala.util.Random
55
*
66
* @see [[scala.collection.Seq]]
77
*/
8-
class ConfigurableLunchDecision(choices: Seq[String]) {
9-
def decide() = choices(Random.nextInt(choices.size))
8+
class Lunch(where: Seq[String]) {
9+
def decide(): String = where(Random.nextInt(where.size))
1010
}
1111

12-
val configured = new ConfigurableLunchDecision(
12+
new Lunch(
1313
Seq(
1414
"Pasta",
1515
"Curry Wurst",

src/main/worksheets/c_oop/b_constructors_2_auxiliary.sc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import scala.util.Random
33
/**
44
* Inheritance and Auxiliary constructors
55
*/
6-
class LimitedChoicesLunchDecision(choices: Seq[String]) {
7-
def decide() = choices(Random.nextInt(choices.size))
6+
class Lunch(choices: Seq[String]) {
7+
def decide(): String = choices(Random.nextInt(choices.size))
88

99
// Restriction:
1010
// The primary constructor must be called from every other constructor
@@ -13,4 +13,4 @@ class LimitedChoicesLunchDecision(choices: Seq[String]) {
1313
}
1414
}
1515

16-
val limited = new LimitedChoicesLunchDecision("Pasta", "Pizza").decide()
16+
new Lunch("Pasta", "Pizza").decide()

src/main/worksheets/c_oop/b_constructors_3_default_parameters.sc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import scala.util.Random
33
/**
44
* Named parameter and default value
55
*/
6-
class TolerantLunchDecision(choice1: String = "Pasta", choice2: String = "Pizza") {
6+
class Lunch(choice1: String = "Pasta", choice2: String = "Pizza") {
77
def decide() = Seq(choice1, choice2)(Random.nextInt(2))
88
}
99

10-
val tolerant = new TolerantLunchDecision(choice2 = "Burger").decide()
10+
// Audience: I want Burger instead of Pizza
11+
new Lunch(choice2 = "Burger").decide()

src/main/worksheets/c_oop/b_constructors_4_constraints.sc

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@
22
/**
33
* Constructor with constraints
44
*/
5-
class NoPastaLunchDecision(val choice: String) {
5+
class NoPasta(val choice: String) {
66
if (choice == "Pasta")
77
throw new IllegalArgumentException("Oh no ... Pasta again!!!")
88
}
99

10-
val noPasta = try {
11-
new NoPastaLunchDecision("Pasta").choice
12-
} catch {
13-
case e: IllegalArgumentException => new NoPastaLunchDecision("Pizza").choice
14-
}
10+
new NoPasta("Pizza")
11+
new NoPasta("Pasta")

src/main/worksheets/c_oop/b_constructors_5_val_var_override.sc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class Reservation(budget: Int, val restaurant: String, var howMany: Int) {
1212

1313
val lunchReservation = new Reservation(250, "Il Ritrovo", 12)
1414

15+
// TODO: think about audience interaction instead of documenting
1516
// TODO: add parameter with override
1617

1718
// Without "val" or "var" we just define a parameter. Accessing a parameter does not work.
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
21
/**
32
* Abstract class with field and methods
4-
*
5-
* [[https://en.wikipedia.org/wiki/Date_format_by_country]]
63
*/
7-
abstract class Date(val day: Int, val month: Int, val year: Int) {
4+
abstract class Date(day: Int, month: Int, year: Int) {
85
def format: String
96
}
107

@@ -13,4 +10,4 @@ class DMY(day: Int, month: Int, year: Int) extends Date(day, month, year) {
1310
override def format: String = s"$day-$month-$year"
1411
}
1512

16-
val dmy = new DMY(1, 1, 1978).format
13+
new DMY(1, 1, 1978).format

src/main/worksheets/c_oop/c_traits_2_definition.sc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ trait JsonAble {
1313
*/
1414
class Date(val day: Int, val month: Int, val year: Int) extends JsonAble {
1515
// @formatter:off
16-
def toJson =
16+
def toJson:String =
1717
s"""
1818
|{
1919
|"year": "$year",

src/main/worksheets/c_oop/c_traits_3_method_name_clashes.sc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ trait B {
1111

1212
class C
1313

14+
// Audience: What happens?
1415
val c = new C() with A with B {
1516
override def foo = super[A].foo
1617
}

0 commit comments

Comments
 (0)