blob: 2db4df002ef3f778252e89068874aec5a394954e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
Multi-values in switch-case
---------------------------
Simple but maybe naive solution:
case 1
case 2
case 3
... # what if this is commented out?
case 4
...
case 5
...
This is tricky, because it could lead to inadvertent fallthrough if
the case "body" is commented out. Some languages have an explicit `break` or
`end` to avoid that.
C style:
case 1:
case 2:
case 3:
...
break;
case 4:
...
break;
case 5:
...
break;
Pascal-style:
case 1..3
begin
...
end;
case 4
begin
...
break;
case 5
begin
...
end;
Gambas/Basic-style:
case to 0
...report error...
case to 3
...
case 4
...
case 5
...
Using special multi-case syntax:
case
1
2
3
then
...
case 4
...
case 5
...
Putting multiple items in one line:
(can be hard to read with many items, and how to wrap lines?)
case 1 2 3
...
case 4
...
case 5
Multi-case with `or` keyword:
case 1
or 2
or 3
...
case 4
...
case 5
...
Or with `also` for better alignment (best solution?):
(this can be combined with multiple items per line)
case 1
also 2
also 3
...
case 4
...
case 5
...
Advanced switch case expressions
--------------------------------
The Gambas programming language has patterns. From it's documentation:
case like "*.{png,jpg,jpeg,bmp}"
Another way is to allow function calls as the case expr:
case pattern "*.bmp"
...
case regex "*\.(jpe?g|webp)"
...
To allow for show identifiers like `pattern`, `regex` or `like`, it
could search in the namespace of a special generic class, such that
different types can have different namespaces.
E.g. `CaseMatch of String` for strings and `CaseMatch of int` for
integers (and `CaseMatch of long` for long, etc.).
Perhaps there could also be some optimization to match against multiple
values efficiently. Maybe there could be a `MulitCaseMatch of Xxx` which
could be instantiated with the value once, and then efficiently compared
against all values in one go.
There's also the matter of compilation of patterns. Not sure how to
implement that, because SLUL isn't going to have static initialization.
Maybe it could be done with CTFE (compile-time function evaluation)?
But then the pre-compilation optimization cannot be improved with
library upgrades.
|