diff --git a/.gitignore b/.gitignore index b13c80e619..22a496be82 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,5 @@ _site *~ vendor/bundle .idea/ +/coursier +/tut-tmp/ diff --git a/tutorials/tour/basics.md b/tutorials/tour/basics.md index 82b1afc3ce..1afe78d1cf 100644 --- a/tutorials/tour/basics.md +++ b/tutorials/tour/basics.md @@ -14,13 +14,13 @@ In this page, we will cover basics of Scala. ## Trying Scala in the Browser -You can run Scala in your browser by using [ScalaFiddle](https://scalafiddle.io). +You can run Scala in your browser with ScalaFiddle. -1. Go to https://scalafiddle.io. -2. Copy and paste `println("Hello, world!")` to the left pane. -3. Hit "Run" button and see it prints "Hello, world!" on the right pane. +1. Go to [https://scalafiddle.io](https://scalafiddle.io). +2. Paste `println("Hello, world!")` in the left pane. +3. Hit "Run" button. Output appears in the right pane. -It is a perfect way for anybody to experiment with a piece of Scala code anywhere, anytime! +This is an easy, zero-setup way to experiment with pieces of Scala code. ## Expressions @@ -28,7 +28,7 @@ Expressions are computable statements. You can output results of expressions using `println`. -``` +```tut println(1) // 1 println(1 + 1) // 2 println("Hello!") // Hello! @@ -37,51 +37,52 @@ println("Hello," + " world!") // Hello, world! ### Values -You can name results of expressions by using the `val` keyword. +You can name results of expressions with the `val` keyword. -``` +```tut val x = 1 + 1 println(x) // 2 ``` -Named results are called values. For example, in above case, `x` is called a value. Values only hold results of expressions, so they will not re-compute expressions when referenced. +Named results, such as `x` here, are called values. Referencing +a value does not re-compute it. Values cannot be re-assigned. -``` +```tut:nofail val x = 1 + 1 x = 3 // This does not compile. ``` -Types of values can be inferred, but you can also explicitly state types like below. +Types of values can be inferred, but you can also explicitly state the type, like this: -``` +```tut val x: Int = 1 + 1 ``` ### Variables -Variables are similar to values except you can re-assign results they hold. You can define variables with the `var` keyword. +Variables are like values, except you can re-assign them. You can define a variable with the `var` keyword. -``` +```tut var x = 1 + 1 x = 3 // This compiles because "x" is declared with the "var" keyword. -println(x) // 3 +println(x * x) // 9 ``` -Just like values, you can also explicitly state types of variables like below. +As with values, you can explicitly state the type if you want: -``` +```tut var x: Int = 1 + 1 ``` ### Blocks -You can create a single expression out of multiple statements by wrapping them with `{}`. We call them blocks or block expressions. +You can combine expressions by surrounding them with `{}`. We call this a block. -The result of the last statement of the block will be the result of the overall block. +The result of the last expression in the block is the result of the overall block, too. -``` +```tut println({ val x = 1 + 1 x + 1 @@ -90,33 +91,33 @@ println({ ## Functions -Functions are expressions with parameters. +Functions are expressions that take parameters. -You can define a function that returns a given integer + 1 like below. +You can define a function that returns a given integer plus one: -``` +```tut (x: Int) => x + 1 ``` -Left hand side of `=>` is a list of parameters and right hand side of `=>` is an expression taking the parameters. +On the left of `=>` is a list of parameters. On the right is an expression involving the parameters. You can also name functions. -``` +```tut val addOne = (x: Int) => x + 1 println(addOne(1)) // 2 ``` -Or it can take multiple parameters. +Functions may take multiple parameters. -``` +```tut val add = (x: Int, y: Int) => x + y println(add(1, 2)) // 3 ``` Or it can take no parameters. -``` +```tut val getTheAnswer = () => 42 println(getTheAnswer()) // 42 ``` @@ -127,31 +128,31 @@ We will cover functions in depth [later](anonymous-function-syntax.md). Methods look and behave very similar to functions, but there are a few key differences between them. -Methods are defined like below with the `def` keyword followed by its name, a list of parameter groups, return type, and an expression. +Methods are defined with the `def` keyword. `def` is followed by a name, parameter lists, a return type, and a body. -``` +```tut def add(x: Int, y: Int): Int = x + y println(add(1, 2)) // 3 ``` Methods cannot be named with the `val` or `var` keywords. -``` +```tut:nofail def add(x: Int, y: Int): Int = x + y val add2 = add // This does not compile. var add3 = add // This does not compile either. ``` -Methods can take multiple parameter groups. +Methods can take multiple parameter lists. -``` +```tut def addThenMultiply(x: Int, y: Int)(multiplier: Int): Int = (x + y) * multiplier println(addThenMultiply(1, 2)(3)) // 9 ``` -Or no parameter groups at all. +Or no parameter lists at all. -``` +```tut def name: String = System.getProperty("name") println("Hello, " + name + "!") ``` @@ -162,15 +163,16 @@ There are some other differences, but for now, you can think of them as somethin You can define classes with the `class` keyword followed by its name and constructor parameters. -``` -class Greeter(prefix: String, postfix: String) { - def greet(name: String): Unit = println(prefix + name + postfix) +```tut +class Greeter(prefix: String, suffix: String) { + def greet(name: String): Unit = + println(prefix + name + suffix) } ``` -You can instantiate classes with the `new` keyword. +You can make an instance of a class with the `new` keyword. -``` +```tut val greeter = new Greeter("Hello, ", "!") greeter.greet("Scala developer") // Hello, Scala developer! ``` @@ -179,23 +181,23 @@ We will cover classes in depth [later](classes.md). ## Case Classes -Scala has a special type of class called case class that's immutable and compared by value by default. You can define case classes with the `case class` keyword. +Scala has a special type of class called a "case" class. By default, case classes are immutable and compared by value. You can define case classes with the `case class` keywords. -``` +```tut case class Point(x: Int, y: Int) ``` You can instantiate case classes without `new` keyword. -``` +```tut val point = Point(1, 2) val anotherPoint = Point(1, 2) val yetAnotherPoint = Point(2, 2) ``` -And they are compared by values. +And they are compared by value. -``` +```tut if (point == anotherPoint) { println(point + " and " + anotherPoint + " are the same.") } else { @@ -219,10 +221,9 @@ Objects are single instances of their own definitions. You can think of them as You can define objects with the `object` keyword. -``` +```tut object IdFactory { private var counter = 0 - def create(): Int = { counter += 1 counter @@ -230,9 +231,9 @@ object IdFactory { } ``` -You can access objects by referring its name. +You can access an object by referring to its name. -``` +```tut val newId: Int = IdFactory.create() println(newId) // 1 val newerId: Int = IdFactory.create() @@ -243,11 +244,11 @@ We will cover objects in depth [later](singleton-objects.md). ## Traits -Traits define specification of types as signature of fields and methods. +Traits are types containing certain fields and methods. Multiple traits can be combined. You can define traits with `trait` keyword. -``` +```tut trait Greeter { def greet(name: String): Unit } @@ -255,15 +256,16 @@ trait Greeter { Traits can also have default implementations. -``` +```tut trait Greeter { - def greet(name: String): Unit = println("Hello, " + name + "!") + def greet(name: String): Unit = + println("Hello, " + name + "!") } ``` -You can extend traits with the `extends` keyword and override their implementations with the `override` keyword. +You can extend traits with the `extends` keyword and override an implementation with the `override` keyword. -``` +```tut class DefaultGreeter extends Greeter class CustomizableGreeter(prefix: String, postfix: String) extends Greeter { @@ -279,16 +281,21 @@ val customGreeter = new CustomizableGreeter("How are you, ", "?") customGreeter.greet("Scala developer") // How are you, Scala developer? ``` +Here, `DefaultGreeter` extends only a single trait, but it could extend multiple traits. + We will cover traits in depth [later](traits.md). ## Main Method -Main method is an entry point of a program. +The main method is an entry point of a program. The Java Virtual +Machine requires a main method to be named `main` and take one +argument, an array of strings. -Using objects, you can define main methods like below. +Using an object, you can define a main method as follows: -``` +```tut object Main { - def main(args: Array[String]): Unit = println("Hello, Scala developer!") + def main(args: Array[String]): Unit = + println("Hello, Scala developer!") } ```