From 0a248a454034104d29d0b326676c9dad3387c82f Mon Sep 17 00:00:00 2001 From: Travis Lee Date: Mon, 13 Mar 2017 17:57:09 +0100 Subject: [PATCH] small changes to basics. closes #765 --- tutorials/tour/_posts/2017-02-13-basics.md | 39 +++++++++++++--------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/tutorials/tour/_posts/2017-02-13-basics.md b/tutorials/tour/_posts/2017-02-13-basics.md index ff47dc3e9c..89a1efda3c 100644 --- a/tutorials/tour/_posts/2017-02-13-basics.md +++ b/tutorials/tour/_posts/2017-02-13-basics.md @@ -26,7 +26,9 @@ This is an easy, zero-setup way to experiment with pieces of Scala code. ## Expressions Expressions are computable statements. - +``` +1 + 1 +``` You can output results of expressions using `println`. ```tut @@ -61,6 +63,8 @@ Types of values can be inferred, but you can also explicitly state the type, lik val x: Int = 1 + 1 ``` +Notice how the type declaration `Int` comes after the identifier `x`. You also need a `:`. + ### Variables Variables are like values, except you can re-assign them. You can define a variable with the `var` keyword. @@ -77,7 +81,8 @@ As with values, you can explicitly state the type if you want: var x: Int = 1 + 1 ``` -### Blocks + +## Blocks You can combine expressions by surrounding them with `{}`. We call this a block. @@ -94,7 +99,7 @@ println({ Functions are expressions that take parameters. -You can define a function that returns a given integer plus one: +You can define an anonymous function (i.e. no name) that returns a given integer plus one: ```tut (x: Int) => x + 1 @@ -123,7 +128,7 @@ val getTheAnswer = () => 42 println(getTheAnswer()) // 42 ``` -We will cover functions in depth [later](anonymous-function-syntax.md). +We will cover functions in depth [later](anonymous-function-syntax.html). ## Methods @@ -136,13 +141,7 @@ 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. -``` +Notice how the return type is declared _after_ the parameter list and a colon `: Int`. Methods can take multiple parameter lists. @@ -160,6 +159,15 @@ println("Hello, " + name + "!") There are some other differences, but for now, you can think of them as something similar to functions. +Methods can have multi-line expressions as well. +```tut +def getSquareString(input: Double): String = { + val square = input * input + square.toString +} +``` +The last expression in the body is the method's return value. (Scala does have a `return` keyword, but it's rarely used.) + ## Classes You can define classes with the `class` keyword followed by its name and constructor parameters. @@ -170,6 +178,7 @@ class Greeter(prefix: String, suffix: String) { println(prefix + name + suffix) } ``` +The return type of the method `greet` is `Unit`, which says there's nothing meaningful to return. It's used similarly to `void` in Java and C. (A difference is that because every Scala expression must have some value, there is actually a singleton value of type Unit, written (). It carries no information.) You can make an instance of a class with the `new` keyword. @@ -178,7 +187,7 @@ val greeter = new Greeter("Hello, ", "!") greeter.greet("Scala developer") // Hello, Scala developer! ``` -We will cover classes in depth [later](classes.md). +We will cover classes in depth [later](classes.html). ## Case Classes @@ -214,7 +223,7 @@ if (point == yetAnotherPoint) { // Point(1,2) and Point(2,2) are different. ``` -There is a lot more to case classes that we'd like to introduce, and we are convinced you will fall in love with them! We will cover them in depth [later](case-classes.md). +There is a lot more to case classes that we'd like to introduce, and we are convinced you will fall in love with them! We will cover them in depth [later](case-classes.html). ## Objects @@ -241,7 +250,7 @@ val newerId: Int = IdFactory.create() println(newerId) // 2 ``` -We will cover objects in depth [later](singleton-objects.md). +We will cover objects in depth [later](singleton-objects.html). ## Traits @@ -284,7 +293,7 @@ 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). +We will cover traits in depth [later](traits.html). ## Main Method