icon-cookie
The website uses cookies to optimize your user experience. Using this website grants us the permission to collect certain information essential to the provision of our services to you, but you may change the cookie settings within your browser any time you wish. Learn more
I agree
Summary | 31 Annotations
The while and do-while constructs are called "loops," not expressions, because they don't result in an interesting value.
2018/10/03 07:11
Because no equals sign precedes its body, greet is defined to be a procedure with a result type of Unit.
2018/10/03 07:19
greet() == ()    
2018/10/03 07:21
Boolean = true
2018/10/03 07:21
in Scala assignment always results in the unit value, (). Thus, the value of the assignment "line = readLine()" will always be () and never be ""
2018/10/03 07:23
With the "file <- filesHere" syntax, which is called a generator
2018/10/03 07:28
, we iterate through the elements of filesHere
2018/10/03 07:28
You can do this with a for expression by adding a filter: an if clause inside the for's parentheses.
2018/10/03 07:31
 if file.getName.endsWith(".scala")
2018/10/03 07:31
You can include more filters if you want.
2018/10/03 07:32
Just keep adding if clauses.
2018/10/03 07:32
If you add more than one filter on a generator, the filter's if clauses must be separated by semicolons.
2018/10/03 07:32
;
2018/10/03 07:32
If you add multiple <- clauses, you will get nested "loops."
2018/10/03 07:35
file <- filesHere
2018/10/03 07:35
line <- fileLines(file)
2018/10/03 07:35
You can do this by binding the result to a new variable using an equals sign (=). The bound variable is introduced and used just like a val, only with the val keyword left out
2018/10/03 07:37
trimmed = line.trim
2018/10/03 07:37
If you prefer, you can use curly braces instead of parentheses to surround the generators and filters. One advantage to using curly braces is that you can leave off some of the semicolons that are needed when you use parentheses.
2018/10/03 07:37
{
2018/10/03 07:37
}
2018/10/03 07:37
yield
2018/10/03 07:38
The syntax of a for-yield expression is like this: for clauses yield body
2018/10/03 07:39
Technically, an exception throw has type Nothing.
2018/10/03 07:44
throw is an expression that has a result type.
2018/10/03 07:45
case ex: FileNotFoundException => // Handle missing file
2018/10/03 07:46
case ex: IOException => // Handle other I/O error
2018/10/03 07:46
Scala does not require you to catch checked exceptions, or declare them in a throws clause
2018/10/03 07:47
finally
2018/10/03 07:47
catch
2018/10/03 07:47
The value computed in the finally clause, if there is one, is dropped.
2018/10/03 07:52