Skip to content

Add code tabs for _tour/annotations #2566

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 4 commits into from
Oct 5, 2022
Merged
Changes from 2 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
67 changes: 64 additions & 3 deletions _tour/annotations.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,39 @@ redirect_from: "/tutorials/tour/annotations.html"
---

Annotations associate meta-information with definitions. For example, the annotation `@deprecated` before a method causes the compiler to print a warning if the method is used.
```

{% tabs annotations_1 class=tabs-scala-version %}
{% tab 'Scala 2' for=annotations_1 %}
```scala mdoc:fail
object DeprecationDemo extends App {
@deprecated("deprecation message", "release # which deprecates method")
def hello = "hola"

hello
}
```
{% endtab %}
{% tab 'Scala 3' for=annotations_1 %}
```scala
object DeprecationDemo extends App:
@deprecated("deprecation message", "release # which deprecates method")
def hello = "hola"

hello
```
{% endtab %}
{% endtabs %}

This will compile but the compiler will print a warning: "there was one deprecation warning".

An annotation clause applies to the first definition or declaration following it. More than one annotation clause may precede a definition and declaration. The order in which these clauses are given does not matter.


## Annotations that ensure correctness of encodings
Certain annotations will actually cause compilation to fail if a condition(s) is not met. For example, the annotation `@tailrec` ensures that a method is [tail-recursive](https://en.wikipedia.org/wiki/Tail_call). Tail-recursion can keep memory requirements constant. Here's how it's used in a method which calculates the factorial:

{% tabs annotations_2 class=tabs-scala-version %}
{% tab 'Scala 2' for=annotations_2 %}
```scala mdoc
import scala.annotation.tailrec

Expand All @@ -38,8 +56,26 @@ def factorial(x: Int): Int = {
factorialHelper(x, 1)
}
```
The `factorialHelper` method has the `@tailrec` which ensures the method is indeed tail-recursive. If we were to change the implementation of `factorialHelper` to the following, it would fail:
{% endtab %}
{% tab 'Scala 3' for=annotations_2 %}
```scala
import scala.annotation.tailrec

def factorial(x: Int): Int =

@tailrec
def factorialHelper(x: Int, accumulator: Int): Int =
if x == 1 then accumulator else factorialHelper(x - 1, accumulator * x)
factorialHelper(x, 1)
```
{% endtab %}
{% endtabs %}

The `factorialHelper` method has the `@tailrec` which ensures the method is indeed tail-recursive. If we were to change the implementation of `factorialHelper` to the following, it would fail:

{% tabs annotations_3 class=tabs-scala-version %}
{% tab 'Scala 2' for=annotations_3 %}
```scala mdoc:fail
import scala.annotation.tailrec

def factorial(x: Int): Int = {
Expand All @@ -50,12 +86,37 @@ def factorial(x: Int): Int = {
factorialHelper(x)
}
```
We would get the message "Recursive call not in tail position".
{% endtab %}
{% tab 'Scala 3' for=annotations_3 %}
```scala
import scala.annotation.tailrec

def factorial(x: Int): Int =
@tailrec
def factorialHelper(x: Int): Int =
if x == 1 then 1 else x * factorialHelper(x - 1)
factorialHelper(x)
```
{% endtab %}
{% endtabs %}

We would get the message "Recursive call not in tail position".

## Annotations affecting code generation

{% tabs annotations_4 class=tabs-scala-version %}
{% tab 'Scala 2' for=annotations_4 %}

Some annotations like `@inline` affect the generated code (i.e. your jar file might have different bytes than if you hadn't used the annotation). Inlining means inserting the code in a method's body at the call site. The resulting bytecode is longer, but hopefully runs faster. Using the annotation `@inline` does not ensure that a method will be inlined, but it will cause the compiler to do it if and only if some heuristics about the size of the generated code are met.

{% endtab %}
{% tab 'Scala 3' for=annotations_4 %}

Some annotations like `@main` affect the generated code (i.e. your jar file might have different bytes than if you hadn't used the annotation). Inlining means inserting the code in a method's body at the call site. A `@main` annotation on a method turns this method into an executable program.

{% endtab %}
{% endtabs %}

### Java Annotations ###
When writing Scala code which interoperates with Java, there are a few differences in annotation syntax to note.
**Note:** Make sure you use the `-target:jvm-1.8` option with Java annotations.
Expand Down