Skip to content

small changes to basics #751

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 27, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 24 additions & 15 deletions tutorials/tour/_posts/2017-02-13-basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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.

Expand All @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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.

Expand All @@ -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.
Expand All @@ -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.

Expand All @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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

Expand Down