You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Closes#615Closes#616
### Summary of Changes
Add list and map literals. The computation of the element & key/value
types will happen in a later pull request, together with type
computation of other type parameters
(#23).
This PR also removes `vararg` parameters in favor of a parameter with
list types. This simplifies the graphical view and gets rid of a bunch
of validation rules.
---------
Co-authored-by: megalinter-bot <129584137+megalinter-bot@users.noreply.github.com>
Copy file name to clipboardexpand all lines: docs/language/common/parameters.md
+6-41
Original file line number
Diff line number
Diff line change
@@ -2,9 +2,8 @@
2
2
3
3
_Parameters_ define the expected inputs of some declaration that can be [called][calls]. We refer to such declarations as _callables_. We distinguish between
4
4
5
-
-[required parameters](#required-parameters), which must always be passed,
6
-
-[optional parameters](#optional-parameters), which use a default value if no value is passed explicitly, and
7
-
-[variadic parameters](#variadic-parameters), which can accept zero or more values.
5
+
-[required parameters](#required-parameters), which must always be passed, and
6
+
-[optional parameters](#optional-parameters), which use a default value if no value is passed explicitly.
8
7
9
8
## Required Parameters
10
9
@@ -36,21 +35,6 @@ These are the syntactic elements:
36
35
- An equals sign.
37
36
- 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.
38
37
39
-
## Variadic Parameters
40
-
41
-
_Variadic parameters_ can consume arbitrarily many [arguments][calls]. Here is an example:
42
-
43
-
```txt
44
-
vararg variadicParameter: Int
45
-
```
46
-
47
-
Let us break down the syntax:
48
-
49
-
- The keyword `vararg`
50
-
- The name of the parameter (here `variadicParameter`). 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.
51
-
- A colon.
52
-
- The [type][types] of the parameter (here `Int`).
53
-
54
38
## Complete Example
55
39
56
40
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):
@@ -72,8 +56,6 @@ The interesting part is the list of parameters, which uses the following syntact
72
56
Several restrictions apply to the order of parameters and to combinations of the various categories of parameters:
73
57
74
58
- After an [optional parameter](#optional-parameters) all parameters must be optional.
75
-
- A single [variadic parameter](#variadic-parameters) can be added at the end of the parameter list.
76
-
- Implied by this: A callable cannot have both [optional parameters](#optional-parameters) and [variadic parameters](#variadic-parameters).
77
59
78
60
## Corresponding Python Code
79
61
@@ -83,7 +65,7 @@ Parameters must be ordered the same way in Python as they are in Safe-DS. Moreov
83
65
84
66
- Name
85
67
- Type
86
-
-Kind (required vs. optional vs. variadic)
68
+
-Optionality (required vs. optional)
87
69
- Default value for optional parameters
88
70
89
71
Let's look at these elements in turn.
@@ -114,19 +96,17 @@ In this case, the Safe-DS parameters `xPred` and `xTest` refer to the Python par
114
96
115
97
The Safe-DS type of a parameter should capture the legal values of this parameter accurately. Ideally, the Python parameter should also have a matching [type hint][types-python].
116
98
117
-
### Matching Kind
99
+
### Matching Optionality
118
100
119
101
Parameters kinds must match on the Safe-DS and Python sides as well. Concretely, this means:
120
102
121
103
- All required parameters in Safe-DS must be required in Python.
122
104
- All optional parameters in Safe-DS must be optional in Python.
123
-
- All variadic parameters in Safe-DS must be variadic in Python (`*args`).
124
105
125
106
Moreover, it must be possible to pass
126
107
127
-
- required parameters by position,
128
-
- optional parameters by name,
129
-
- variadic parameters by position.
108
+
- required parameters by position, and
109
+
- optional parameters by name.
130
110
131
111
These rules allow us to restrict required parameters to [positional-only][python-positional-only] or optional parameters to [keyword-only][python-keyword-only]. We can also keep both unrestricted.
132
112
@@ -162,21 +142,6 @@ def optional(a: int = 1):
162
142
fun optional(a: Int = 1)
163
143
```
164
144
165
-
#### Variadic Parameter
166
-
167
-
```py
168
-
# Python code
169
-
170
-
defvariadic(*a: int):
171
-
pass
172
-
```
173
-
174
-
```txt
175
-
// Safe-DS code
176
-
177
-
fun variadic(vararg a: Int)
178
-
```
179
-
180
145
### Matching Default Value
181
146
182
147
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:
Copy file name to clipboardexpand all lines: docs/language/pipeline-language/expressions.md
+7-9
Original file line number
Diff line number
Diff line change
@@ -219,7 +219,6 @@ There are some restriction regarding the choice of positional vs. named argument
219
219
- For all [parameters][parameters] of the callee there must be at most one argument.
220
220
- For all [required parameters][required-parameters] there must be exactly one argument.
221
221
- After a named argument all arguments must be named.
222
-
-[Variadic parameters][variadic-parameters] can only be assigned by position.
223
222
224
223
### Legal Callees
225
224
@@ -402,17 +401,17 @@ createValueWrapper()
402
401
403
402
## Indexed Accesses
404
403
405
-
An indexed access is currently only used to access one value assigned to a [variadic parameter][variadic-parameters]. In the following example, we use an index access to retrieve the first value that is assigned to the [variadic parameter][variadic-parameters]`values`of the segment `printFirst`:
404
+
An indexed access is currently only used to access elements of a list or values of a map by their key. In the following example, we use an index access to retrieve the first element of the `values`list:
406
405
407
406
```txt
408
-
segment printFirst(vararg values: Int) {
407
+
segment printFirst(values: List<Int>) {
409
408
print(values[0]);
410
409
}
411
410
```
412
411
413
412
These are the elements of the syntax:
414
413
415
-
-The name of the variadic parameter (here `values`).
414
+
-An expression that evaluates to a list or map (here the [reference](#references)`values`).
416
415
- An opening square bracket.
417
416
- The index, which is an expression that evaluates to an integer. The first element has index 0.
418
417
- A closing square bracket.
@@ -434,16 +433,16 @@ This is a [class][classes] `LinearRegression`, which has a constructor and an in
434
433
We can then use those declarations in a [segment][segments]:
This segment is called `mySegment` and has a [variadic parameter][variadic-parameters]`regressions` of type `LinearRegression`. This means we can pass an arbitrary number of instances of `LinearRegression` to the segment when we [call](#calls) it.
441
+
This segment is called `mySegment` and has a [parameter][parameters]`regressions` of type `List<LinearRegression>`.
443
442
444
443
In the body of the segment we then
445
444
446
-
1. access the first instance that was pass using an [indexed access](#indexed-accesses),
445
+
1. access the first instance in the list using an [indexed access](#indexed-accesses),
447
446
2. access the instance method `drawAsGraph` of this instance using a [member access](#member-accesses),
448
447
3.[call](#calls) this method.
449
448
@@ -456,7 +455,7 @@ class IntList {
456
455
fun filter(filterFunction: (element: Int) -> shouldKeep: Boolean) -> filteredList: IntList
457
456
}
458
457
459
-
fun intListOf(vararg elements: Int) -> result: IntList
458
+
fun intListOf(elements: List<Int>) -> result: IntList
460
459
```
461
460
462
461
First, we declare a [class][classes]`IntList`, which has a single [method][methods] called `filter`. The `filter` method returns a single result called `filteredList`, which is a new `IntList`. `filteredList` is supposed to only contain the elements of the receiving `IntList` for which the `filterFunction`[parameter][parameters] returns `true`.
@@ -566,7 +565,6 @@ If the default precedence of operators is not sufficient, parentheses can be use
0 commit comments