Skip to content

Commit

Permalink
docs: highlight inline code (#654)
Browse files Browse the repository at this point in the history
### Summary of Changes

Highlight Safe-DS inline code in the documentation.
  • Loading branch information
lars-reimann authored Oct 20, 2023
1 parent fe8c602 commit 594c2c8
Show file tree
Hide file tree
Showing 22 changed files with 1,028 additions and 293 deletions.
2 changes: 1 addition & 1 deletion docs/language/common/comments.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ comment
*/
```

They are started with `/*` and end at the inverted delimiter `*/`. There must be no space between the slash and the star. Everything in between the delimiters is the text of the comment.
They are started with `#!sds /*` and end at the inverted delimiter `#!sds */`. There must be no space between the slash and the star. Everything in between the delimiters is the text of the comment.
18 changes: 9 additions & 9 deletions docs/language/common/imports.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ from safeds.model.regression import DecisionTree as StdlibDecisionTree

Let us take apart the syntax:

- The keyword `from`.
- The keyword `#!sds from`.
- The name of the [package][packages] that contains the declaration (here `safeds.model.regression`).
- The keyword `import`.
- The keyword `#!sds import`.
- The name of the declaration (i.e. `DecisionTree`).
- The keyword `as`.
- The keyword `#!sds as`.
- The alias to use (here `StdlibDecisionTree`). This can be any combination of upper- and lowercase letters, underscores, and numbers, as long as it does not start with a number.

Afterwards, the declaration can **only** be accessed using the alias. The next example shows how to create a new instance of the class now by invoking its constructor:
Expand All @@ -64,28 +64,28 @@ from safeds.model.regression import DecisionTree as StdlibDecisionTree, RandomFo

We can also import all declarations in a [package][packages] with a single import. While this saves some typing, it also increases the likelihood of name conflicts and can make it harder for readers of the code to determine where a declaration comes from. Therefore, this should be used with caution.

Nevertheless, let us look at an example, which imports all declarations from the [package][packages] `safeds.model.regression`:
Nevertheless, let us look at an example, which imports all declarations from the [package][packages] `#!sds safeds.model.regression`:

```sds
from safeds.model.regression import *
```

Here is the breakdown of the syntax:

- The keyword `from`.
- The name of the [package][packages] to import (here `safeds.model.regression`).
- The keyword `import`.
- The keyword `#!sds from`.
- The name of the [package][packages] to import (here `#!sds safeds.model.regression`).
- The keyword `#!sds import`.
- A star.

Afterwards, we can again access declarations by their simple name, such as the [class][classes] `DecisionTree`:
Afterward, we can again access declarations by their simple name, such as the [class][classes] `#!sds DecisionTree`:

```sds
DecisionTree()
```

[Aliases](#qualified-imports-with-alias) cannot be used together with wildcard imports.

Note that declarations in subpackages, i.e. packages that have a different name but the same prefix as the imported one, are **not** imported. Therefore, if we would only `import safeds.model`, we could no longer access the [class][classes] `DecisionTree`.
Note that declarations in subpackages, i.e. packages that have a different name but the same prefix as the imported one, are **not** imported. Therefore, if we would instead write `#!sds from safeds.model import *`, we could no longer access the [class][classes] `#!sds DecisionTree`.

[classes]: ../stub-language/classes.md
[packages]: packages.md
Expand Down
32 changes: 16 additions & 16 deletions docs/language/common/packages.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ package de.unibonn.speedPrediction

It has these syntactic elements:

- The keyword `package`.
- The name of the package, which consists of multiple _segments_ separated by dots. Each segment can be any combination of upper- and lowercase letters, underscores, and numbers, as long as it does not start with a number. However, we suggest to use `lowerCamelCase` for all segments. We also recommend to use the [reverse domain name notation](https://en.wikipedia.org/wiki/Reverse_domain_name_notation) for the package name.
- The keyword `#!sds package`.
- The name of the package, which consists of multiple _segments_ separated by dots. Each segment can be any combination of upper- and lowercase letters, underscores, and numbers, as long as it does not start with a number. However, we suggest to use `#!sds lowerCamelCase` for all segments. We also recommend to use the [reverse domain name notation](https://en.wikipedia.org/wiki/Reverse_domain_name_notation) for the package name.

Multiple files can belong to the same package but each non-empty file must declare its package before any [import][imports] or declarations. Moreover, within the same package the names of declarations must be unique.

Expand All @@ -33,7 +33,7 @@ class DecisionTree:
pass # Implementation omitted
```

This file contains the actual implementation of the Python class `DecisionTree`. We now want to make this Python class available in Safe-DS, which requires the following Safe-DS stub file:
This file contains the actual implementation of the Python class `#!py DecisionTree`. We now want to make this Python class available in Safe-DS, which requires the following Safe-DS stub file:

```sds
// Safe-DS file "safeds/model/regression/_decision_tree/DecisionTree.sdsstub"
Expand All @@ -51,13 +51,13 @@ Note that the Safe-DS package corresponds to the path to the Python file, where
from safeds.model.regression._decision_tree import DecisionTree
```

The part between `from` and `import` is exactly what the Safe-DS package has to be.
The part between `#!py from` and `#!py import` is exactly what the Safe-DS package has to be.

The file path is irrelevant in Safe-DS. For the sake of code organization, however, we advise to use the segments of the package name to create a folder hierarchy and then create a Safe-DS stub file with some fitting name in there.

### File Imports Declaration

The second option is to chose the Safe-DS package that corresponds to a [Python module][python-modules] that imports the declaration we are interested in. This is particularly useful if multiple [Python modules][python-modules] are bundled into a [Python package][python-packages]. In the `__init__.py` file we can then bundle the declarations from different [Python module][python-modules] together.
The second option is to chose the Safe-DS package that corresponds to a [Python module][python-modules] that imports the declaration we are interested in. This is particularly useful if multiple [Python modules][python-modules] are bundled into a [Python package][python-packages]. In the `#!sds __init__.py` file we can then bundle the declarations from different [Python module][python-modules] together.

We will demonstrate this by extending the example we used above:

Expand All @@ -75,15 +75,15 @@ class DecisionTree:
from ._decision_tree import DecisionTree
```

The addition of the `__init__.py` file now allows us to import the Python class `DecisionTree` in another way in Python client code:
The addition of the `__init__.py` file now allows us to import the Python class `#!py DecisionTree` in another way in Python client code:

```py
# Python

from safeds.model.regression import DecisionTree
```

Note the omission of the suffix `._decision_tree` after `safeds.model.regression`. Likewise, we can now update the Safe-DS stub code. We again just take everything between `from` and `import` and use this as the Safe-DS package name:
Note the omission of the suffix `#!py ._decision_tree` after `#!py safeds.model.regression`. Likewise, we can now update the Safe-DS stub code. We again just take everything between `#!py from` and `#!py import` and use this as the Safe-DS package name:

```sds
// Safe-DS file "safeds/model/regression/DecisionTree.sdsstub"
Expand All @@ -95,13 +95,13 @@ class DecisionTree()

Generally, this method is preferable to our initial solution in the Section ["File Contains Implementation"](#file-contains-implementation), since we hide the detail in which file a declaration resides. This makes later refactorings easier since we can freely move declarations between files in the same Python package without breaking client code.

### `@PythonModule` Annotation
### `#!sds @PythonModule` Annotation

Choosing the Safe-DS package according to the rules described above is essential for code generation to work properly. However, we might sometimes get warnings related to the Safe-DS naming convention, which wants the segments of the Safe-DS package to be `lowerCamelCase`. We now have several options:
Choosing the Safe-DS package according to the rules described above is essential for code generation to work properly. However, we might sometimes get warnings related to the Safe-DS naming convention, which wants the segments of the Safe-DS package to be `#!sds lowerCamelCase`. We now have several options:

- If the declaration is not part of the Safe-DS standard library, we can ignore the warning.
- We can update the Python code.
- We can use the `@PythonModule` annotation.
- We can use the `#!sds @PythonModule` annotation.

The first two options are self-explanatory. Let us now look at the third one. We use our initial example from the Section ["File Contains Implementation"](#file-contains-implementation) again:

Expand All @@ -113,7 +113,7 @@ class DecisionTree:
pass # Implementation omitted
```

Our original solution leads to a warning because the Safe-DS package name contains the segment `_decision_tree`, which is not `lowerCamelCase` due to the underscores:
Our original solution leads to a warning because the Safe-DS package name contains the segment `#!sds _decision_tree`, which is not `#!sds lowerCamelCase` due to the underscores:

```sds
// Safe-DS file "safeds/model/regression/_decision_tree/DecisionTree.sdsstub"
Expand All @@ -123,7 +123,7 @@ package safeds.model.regression._decision_tree
class DecisionTree()
```

By [calling][annotation-calls] the [annotation][annotations] `@PythonModule`, we can also specify the corresponding [Python module][python-modules], however. If this [annotation][annotations] is [called][annotation-calls], it takes precedence over the Safe-DS package name. This allows us to pick an arbitrary Safe-DS package that respects the Safe-DS naming convention. We can even group multiple [Python modules][python-modules] together in one Safe-DS package without relying on Python's `__init__.py` files:
By [calling][annotation-calls] the [annotation][annotations] `#!sds @PythonModule`, we can also specify the corresponding [Python module][python-modules], however. If this [annotation][annotations] is [called][annotation-calls], it takes precedence over the Safe-DS package name. This allows us to pick an arbitrary Safe-DS package that respects the Safe-DS naming convention. We can even group multiple [Python modules][python-modules] together in one Safe-DS package without relying on Python's `#!sds __init__.py` files:

```sds
// Safe-DS file "safeds/model/regression/DecisionTree.sdsstub"
Expand All @@ -137,16 +137,16 @@ class DecisionTree()

Here is a breakdown of this:

- We [call][annotation-calls] the `@PythonModule` [annotation][annotations] before we declare the Safe-DS package. The [Python module][python-modules] that exports the Python declarations that correspond to the Safe-DS declarations in this stub file is passed as a [string literal][string-literals] (here `safeds.model.regression._decision_tree`). This is used only for code generation and does not affect users of Safe-DS.
- We [call][annotation-calls] the `#!sds @PythonModule` [annotation][annotations] before we declare the Safe-DS package. The [Python module][python-modules] that exports the Python declarations that correspond to the Safe-DS declarations in this stub file is passed as a [string literal][string-literals] (here `#!sds safeds.model.regression._decision_tree`). This is used only for code generation and does not affect users of Safe-DS.
- We specify the Safe-DS package as usual. This must be used when we [import][imports] the declaration in another Safe-DS file:

```sds
`#!sds ``sds
// Safe-DS

from safeds.model.regression import DecisionTree
```
`#!sds ``

It is important to note that the `@PythonModule` annotation only affects the one Safe-DS file that contains it rather than the entire Safe-DS package. This allows different Safe-DS files in the same package to point to different [Python modules][python-modules].
It is important to note that the `#!sds @PythonModule` annotation only affects the one Safe-DS file that contains it rather than the entire Safe-DS package. This allows different Safe-DS files in the same package to point to different [Python modules][python-modules].

[stub-language]: ../stub-language/README.md
[annotations]: ../stub-language/annotations.md
Expand Down
32 changes: 16 additions & 16 deletions docs/language/common/parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ requiredParameter: Int

Here are the pieces of syntax:

- The name of the parameter (here `requiredParameter`). This can be any combination of upper- and lowercase letters, underscores, and numbers, as long as it does not start with a number. However, we suggest to use `lowerCamelCase` for the names of parameters.
- The name of the parameter (here `#!sds requiredParameter`). This can be any combination of upper- and lowercase letters, underscores, and numbers, as long as it does not start with a number. However, we suggest to use `#!sds lowerCamelCase` for the names of parameters.
- A colon.
- The [type][types] of the parameter (here `Int`).
- The [type][types] of the parameter (here `#!sds Int`).

## Optional Parameters

Expand All @@ -29,15 +29,15 @@ optionalParameter: Int = 1

These are the syntactic elements:

- The name of the parameter (here `optionalParameter`). This can be any combination of upper- and lowercase letters, underscores, and numbers, as long as it does not start with a number. However, we suggest to use `lowerCamelCase` for the names of parameters.
- The name of the parameter (here `#!sds optionalParameter`). This can be any combination of upper- and lowercase letters, underscores, and numbers, as long as it does not start with a number. However, we suggest to use `#!sds lowerCamelCase` for the names of parameters.
- A colon.
- The [type][types] of the parameter (here `Int`).
- The [type][types] of the parameter (here `#!sds Int`).
- An equals sign.
- The default value of the parameter (here `1`). This must be a constant expression, i.e. something that can be evaluated by the compiler. Particularly [calls][calls] usually do not fulfill this requirement.
- The default value of the parameter (here `#!sds 1`). This must be a constant expression, i.e. something that can be evaluated by the compiler. Particularly [calls][calls] usually do not fulfill this requirement.

## Complete Example

Let us now look at a full example of a [segment][segments] called `doSomething` with one [required parameter](#required-parameters) and one [optional parameter](#optional-parameters):
Let us now look at a full example of a [segment][segments] called `#!sds doSomething` with one [required parameter](#required-parameters) and one [optional parameter](#optional-parameters):

```sds
segment doSomething(requiredParameter: Int, optionalParameter: Boolean = false) {
Expand Down Expand Up @@ -72,7 +72,7 @@ Let's look at these elements in turn.

### Matching Name

By default, parameter names in Safe-DS must be identical to their names in Python. If this is not desired, for example due to clashing name conventions in Safe-DS and Python, the `@PythonName` annotation can be used to link a Safe-DS parameter to a Python parameter with a different name. Here is an example:
By default, parameter names in Safe-DS must be identical to their names in Python. If this is not desired, for example due to clashing name conventions in Safe-DS and Python, the `#!sds @PythonName` annotation can be used to link a Safe-DS parameter to a Python parameter with a different name. Here is an example:

```py
# Python code
Expand All @@ -90,7 +90,7 @@ fun accuracy(
) -> accuracy: Float
```

In this case, the Safe-DS parameters `xPred` and `xTest` refer to the Python parameters `x_pred` and `x_test` respectively.
In this case, the Safe-DS parameters `#!sds xPred` and `#!sds xTest` refer to the Python parameters `#!py x_pred` and `#!py x_test` respectively.

### Matching Type

Expand Down Expand Up @@ -146,14 +146,14 @@ fun optional(a: Int = 1)

Most commonly, default values in Python are literals, since default values are only evaluated once in Python rather than every time the function is called. The following table shows how Safe-DS literals and Python literals correspond:

| Safe-DS Literal | Python Literal |
|---------------------------------------|------------------------|
| `1` ([int][int-literals]) | `1` |
| `1.0` ([float][float-literals]) | `1.0` |
| `"hello"` ([string][string-literals]) | `"hello"` or `'hello'` |
| `false` ([boolean][boolean-literals]) | `False` |
| `true` ([boolean][boolean-literals]) | `True` |
| `null` ([null][null-literals]) | `None` |
| Safe-DS Literal | Python Literal |
|---------------------------------------|-----------------------------------|
| `#!sds 1` ([int][int-literals]) | `#!py 1` |
| `#!sds 1.0` ([float][float-literals]) | `#!py 1.0` |
| `#!sds "hello"` ([string][string-literals]) | `#!py "hello"` or `#!sds 'hello'` |
| `#!sds false` ([boolean][boolean-literals]) | `#!py False` |
| `#!sds true` ([boolean][boolean-literals]) | `#!py True` |
| `#!sds null` ([null][null-literals]) | `#!py None` |

[types]: types.md
[types-python]: types.md#corresponding-python-code
Expand Down
8 changes: 4 additions & 4 deletions docs/language/common/results.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ result: Int

Here is a breakdown of the syntax:

- The name of the result (here `result`). This can be any combination of upper- and lowercase letters, underscores, and numbers, as long as it does not start with a number. However, we suggest to use `lowerCamelCase` for the names of parameters.
- The name of the result (here `#!sds result`). This can be any combination of upper- and lowercase letters, underscores, and numbers, as long as it does not start with a number. However, we suggest to use `#!sds lowerCamelCase` for the names of parameters.
- A colon.
- The [type][types] of the parameter (here `Int`).
- The [type][types] of the parameter (here `#!sds Int`).

## Complete Example

Let us now look at a full example of a [segment][segments] called `doSomething` with two results:
Let us now look at a full example of a [segment][segments] called `#!sds doSomething` with two results:

```sds
segment doSomething() -> (result1: Int, result2: Boolean) {
Expand All @@ -24,7 +24,7 @@ segment doSomething() -> (result1: Int, result2: Boolean) {

The interesting part is the list of results, which uses the following syntactic elements:

- An arrow `->`.
- An arrow `#!sds ->`.
- An opening parenthesis.
- A list of results, the syntax is as described above. They are separated by commas. A trailing commas is permitted.
- A closing parenthesis.
Expand Down
Loading

0 comments on commit 594c2c8

Please sign in to comment.