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
Copy file name to clipboardExpand all lines: courses/RascalAmendmentProposals/RAP12/RAP12.md
+17-11Lines changed: 17 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,24 +14,26 @@ sidebar_position: 12
14
14
15
15
1. Visit behaves very differently on strings than on other values. Namely when a value of String is reached, it generates all tails of the string and executes all patterns on these tails.
16
16
1. This is considerably different from the visit behavior on other “containers” such as lists, sets and maps. Visit simply traverses each element in isolation and not “all tails”.
17
-
2. The fact that a string is treated as a container is inconsistent with other language feature such as the generator \`\<-\` syntax. There we do not iterate over all tails of a list, for example.
18
-
3. The implementation in the interpreter switches to this special string behavior if the **dynamic typ**e of the \_subject\_ is a string. This contradicts compiler behavior which should select which kind of visit is necessary based on static information.
19
-
4. If you are simply using visit to visit all sub-values, it can be very surprising that the string tail visiting behavior is triggered. For example, when a \`case value x: i+=1;\` simply counts all values in the tree, the total count would be augmented with an increment for every tail of every string (\!\!\!). This is counterintuitive.
17
+
2. The fact that a string is treated as a container is inconsistent with other language feature such as the generator `<-` syntax. There we do not iterate over all tails of a list, for example.
18
+
3. The implementation in the interpreter switches to this special string behavior if the **dynamic typ**e of the _subject_ is a string. This contradicts compiler behavior which should select which kind of visit is necessary based on static information.
19
+
4. If you are simply using visit to visit all sub-values, it can be very surprising that the string tail visiting behavior is triggered. For example, when a `case value x: i+=1;` simply counts all values in the tree, the total count would be augmented with an increment for every tail of every string (\!\!\!). This is counterintuitive.
20
20
21
21
22
22
### Analysis
23
23
24
24
2. There seems to be a case of “too much overloading” of the visit syntax. We do need this kind of power to edit strings concisely, but editing strings is not simply a special case of recursively visiting a data-structure. For strings we need additional semantics (a cursor inside of the string)
25
-
3. It would be easy to separate the functionality into one \`edit\` statement and a \`visit\` statement, where \`visit\` would behave as before but not dive into tails of strings, and \`edit\` would behave as \`visit\` behaves now on strings (namely to visit all tails).
25
+
3. It would be easy to separate the functionality into one `edit` statement and a `visit` statement, where `visit` would behave as before but not dive into tails of strings, and `edit` would behave as `visit` behaves now on strings (namely to visit all tails).
26
26
27
27
### Solution proposal
28
28
29
29
Introduce a new type of statement/expression for editing strings:
30
30
31
-
result \=**edit**(subject) {
32
-
**case** “string” \=\> “string”
33
-
**case** /regex/ \=\> regex
31
+
```rascal
32
+
result = edit(subject) {
33
+
case “string” => “string”
34
+
case /regex/ => regex
34
35
}
36
+
```
35
37
36
38
* The subject should (statically) be a string
37
39
* Cases of an edit statement should be either literal strings or regular expressions
@@ -42,17 +44,21 @@ This is exactly the same as how visit currently works on strings.
42
44
43
45
This example shows what would happen:
44
46
45
-
result \=**edit**(“aaa”) {
46
-
**case** “a” \=\> “b”
47
+
```rascal
48
+
result = edit(“aaa”) {
49
+
case “a” => “b”
47
50
}
51
+
```
48
52
49
53
This would return “bbb”, since “a” matches at all cursor positions.
50
54
51
55
Contrarily the following visit statement would behave differently:
52
56
53
-
result \=**visit** (“aaa”) {
54
-
**case** “a” \=\> “b”
57
+
```rascal
58
+
result = visit (“aaa”) {
59
+
case “a” => “b”
55
60
}
61
+
```
56
62
57
63
Would return “aaa” since no value matched the “a” pattern. Only entire strings “a” would be replaced by “b” and strings such as “aaa” will remain unchanged.
0 commit comments