Skip to content

Commit 6808020

Browse files
committed
Add information about packages and imports
1 parent d9106dd commit 6808020

File tree

3 files changed

+84
-12
lines changed

3 files changed

+84
-12
lines changed

src/main/scala/scalatutorial/ScalaTutorial.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import sections._
1111
object ScalaTutorial extends Library {
1212
val owner = "scala-exercises"
1313
val repository = "exercises-fpprinciples"
14-
override val color = Some("#224951")
14+
override val color = Some("#f26527")
1515
val logoPath = "scala-tutorial"
1616

1717
val sections = List(

src/main/scala/scalatutorial/sections/FunctionalLoops.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,20 @@ object FunctionalLoops extends ScalaTutorialSection {
113113
* def sqrt(x: Double) = sqrtIter(1.0, x)
114114
* }}}
115115
*
116+
* = Summary =
117+
*
118+
* You have seen simple elements of functional programing in Scala.
119+
*
120+
* - arithmetic and boolean expressions
121+
* - conditional expressions if-else
122+
* - functions with recursion
123+
*
124+
* You have learned the difference between the call-by-name and
125+
* call-by-value evaluation strategies.
126+
*
127+
* You have learned a way to reason about program execution: reduce expressions using
128+
* the substitution model.
129+
*
116130
* = Exercise =
117131
*
118132
* Complete the following method definition that computes the factorial of a number:

src/main/scala/scalatutorial/sections/LexicalScopes.scala

Lines changed: 69 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ object LexicalScopes extends ScalaTutorialSection {
136136
* There are two ways to overcome this problem.
137137
*
138138
* You could write the multi-line expression in parentheses, because semicolons
139-
* are never inserted inside `(...)`:
139+
* are never inserted inside `()`:
140140
*
141141
* {{{
142142
* (someLongExpression
@@ -151,20 +151,78 @@ object LexicalScopes extends ScalaTutorialSection {
151151
* someOtherExpression
152152
* }}}
153153
*
154-
* = Summary =
154+
* = Top-Level Definitions =
155155
*
156-
* You have seen simple elements of functional programing in Scala.
156+
* In real Scala programs, `def` and `val` definitions must be written
157+
* within a top-level ''object definition'', in .scala file:
157158
*
158-
* - arithmetic and boolean expressions
159-
* - conditional expressions if-else
160-
* - functions with recursion
161-
* - nesting and lexical scope
159+
* {{{
160+
* object MyExecutableProgram {
161+
* val myVal = …
162+
* def myMethod = …
163+
* }
164+
* }}}
165+
*
166+
* The above code defines an ''object'' named `MyExecutableProgram`. You
167+
* can refer to its ''members'' using the usual dot notation:
168+
*
169+
* {{{
170+
* MyExecutableProgram.myMethod
171+
* }}}
172+
*
173+
* The definition of `MyExecutableProgram` is ''top-level'' because it
174+
* is not nested within another definition.
162175
*
163-
* You have learned the difference between the call-by-name and
164-
* call-by-value evaluation strategies.
176+
* = Packages and Imports =
177+
*
178+
* Top-level definitions can be organized in ''packages'':
179+
*
180+
* {{{
181+
* // file foo/Bar.scala
182+
* package foo
183+
* object Bar { … }
184+
* }}}
185+
*
186+
* {{{
187+
* // file foo/Baz.scala
188+
* package foo
189+
* object Baz { … }
190+
* }}}
165191
*
166-
* You have learned a way to reason about program execution: reduce expressions using
167-
* the substitution model.
192+
* Definitions located in a package are visible from other definitions
193+
* located in the same package:
194+
*
195+
* {{{
196+
* // file foo/Baz.scala
197+
* package foo
198+
* object Baz {
199+
* // Bar is visible because it is in the `foo` package too
200+
* Bar.someMethod
201+
* }
202+
* }}}
203+
*
204+
* On the other hand, definitions located in other packages are not directly
205+
* visible: you must use ''fully qualified names'' to refer to them:
206+
*
207+
* {{{
208+
* // file quux/Quux.scala
209+
* package quux
210+
* object Quux {
211+
* foo.Bar.someMethod
212+
* }
213+
* }}}
214+
*
215+
* Finally, you can import names to avoid repeating their fully qualified form:
216+
*
217+
* {{{
218+
* // file quux/Quux.scala
219+
* package quux
220+
* import foo.Bar
221+
* object Quux {
222+
* // Bar refers to the imported `foo.Bar`
223+
* Bar.someMethod
224+
* }
225+
* }}}
168226
*/
169227
def nothing(): Unit = ()
170228
}

0 commit comments

Comments
 (0)