]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.runtime/src-isv/JavaVsSCL.mediawiki
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.scl.runtime / src-isv / JavaVsSCL.mediawiki
1 == Comparison of Java and SCL ==
2
3 === Language constructions and patterns ===
4
5 <table border="1">
6 <tr>
7 <td><pre>0.5 + x * (2e-3 + x * 2)</pre></td>
8 <td><pre>0.5 + x * (2e-3 + x * 2)</pre></td>
9 </tr>
10
11 <tr>
12 <td><pre>x >= 0 && x <= 1</pre></td>
13 <td><pre>x >= 0 && x <= 1</pre></td>
14 </tr>
15
16 <tr>
17 <td><pre>Math.sin(a1) - Math.cos(a2)</pre></td>
18 <td><pre>sin a1 - cos a2</pre></td></tr>
19
20 <tr>
21 <td><pre>cond ? value : alternative</pre></td>
22 <td><pre><b>if</b> cond <b>then</b> value <b>else</b> alternative</pre></td>
23 </tr>
24
25 <tr>
26 <td><pre>"I am " + age + " years old."</pre></td>
27 <td>
28 <pre>"I am " + show age + " years old."</pre>
29
30 In future:
31
32 <pre>"I am ${age} years old."</pre>
33 </td>
34 </tr>
35
36 <tr><td><pre>
37 <b>final</b> int sum = a + b;</pre>
38 </td><td><pre>
39 sum = a + b</pre>
40 </td></tr>
41
42 <tr><td><pre>
43 public static String decorate(String x) {
44     return "(" + x + ")"; 
45 }</pre>
46 </td><td><pre>
47 decorate :: String -> String
48 decorate x = "(" + x + ")"</pre>
49 (the first line is optional)
50 </td></tr>
51
52 <tr><td><pre>
53 {
54     statement1;
55     statement2;
56     statement3;
57 }</pre>
58 </td><td><pre>
59 <b>do</b>
60     statement1
61     statement2
62     statement3
63 </pre></td></tr>
64
65 <tr><td><pre>
66 <b>if</b>(cond1)
67     statement1;
68 <b>else if</b>(cond2)
69     statement2;
70 <b>else</b> 
71     statement3;
72 </pre></td><td><pre>
73 <b>if</b> cond1
74 <b>then</b> statement1
75 <b>else if</b> cond2
76 <b>then</b> statement2
77 <b>else</b> statement3
78 </pre></td></tr>
79
80 <tr><td><pre>
81 <b>for</b>(T x : l)
82     statement;
83 </pre></td><td><pre>
84 for l (\x -> 
85     statement
86 )
87 </pre></td></tr>
88
89 <tr><td><pre>
90 <b>while</b>(cond) statement;
91 </pre></td><td><pre>
92 while (\() -> cond) (\() -> statement)
93 </pre>
94 In future with improvements to macros:
95 <pre>
96 while cond statement
97 </pre>
98 </td></tr>
99
100 <tr><td><pre>
101 int var;
102 <b>switch</b>(value) {
103 <b>case</b> Label1: 
104     var = case1; 
105     break;
106 <b>case</b> Label2: 
107     var = case2; 
108     break;
109 <b>default</b>:
110     var = case3;        
111 }</pre>
112 </td><td><pre>
113 var = <b>match</b> value <b>with</b>
114     Label1 -> case1
115     Label2 -> case2
116     _      -> case3
117 </pre>
118 </td></tr>
119
120 <tr><td><pre>
121 <b>break</b>;
122 <b>continue</b>;
123 <b>return</b> x; // In the middle of the code
124 </pre></td><td>No exact correspondence. However, every control sequence can be written in the following form:
125 <pre>label1 initialState
126   where
127     label1 state = 
128         ...
129         then label2 modifiedState1
130         else label3 modifiedState2
131     label2 state = 
132         ...
133     label3 state = 
134         ...
135         label1 modifiedState</pre>
136 </td></tr>
137
138 <tr><td><pre>
139 int a, b;
140 <b>if</b>(cond) {
141     a = aExp1;
142     b = bExp1;
143 }
144 <b>else</b> {
145     a = aExp2;
146     b = bExp2;
147 }
148 </pre>
149 </td><td><pre>
150 (a, b) = <b>if</b> cond
151           <b>then</b> (aExp1, bExp1)
152           <b>else</b> (aExp2, bExp2)         
153 </pre>
154 </td></tr>
155
156 <tr><td><pre>
157 int a = 1;
158 a = 2;
159 a = 3;
160 return a;
161 </pre>
162 </td><td><pre>
163 a = ref 1
164 a := 2
165 a := 3
166 getRef a
167 </pre>
168
169 The name of the function <code>getRef</code> will change in the future and is
170 probably replaced by some special syntax. For example in ML <code>getRef a</code>
171 would be written as <code>!a</code>.
172
173 </td></tr>
174
175 <tr><td><pre>
176 public static String f(int x) {
177     if(x >= 0)
178         return Integer.toString(x);
179     else
180         return null;
181 }</pre>
182 </td><td><pre>
183 f :: Integer -> Maybe String
184 f x | x >= 0    = Just (show x)
185      | otherwise = Nothing
186 </pre>
187 </td></tr>
188
189 <tr><td><pre>
190 public static int invF(String s) {
191     if(s == null)
192         return -1;
193     else
194         return Integer.parseInt(s);
195 }</pre>
196 </td><td><pre>
197 invF :: Maybe String -> Integer
198 invF Nothing  = -1
199 invF (Just s) = read s
200 </pre>
201 </td></tr>
202
203 <tr><td><pre>
204 Arrays.asList(new String[] {"x", "y", "z"})
205 </pre>
206 </td><td><pre>
207 [ "x", "y", "z" ]
208 </pre>
209 </td></tr>
210
211 <tr><td><pre>
212 l.get(3)
213 </pre>
214 </td><td><pre>
215 l!3
216 </pre>
217 </td></tr>
218
219 <tr><td><pre>
220 List<String> decorated = new ArrayList<String>();
221 for(String s : undecorated)
222     decorated.add("(" + s + ")");
223 </pre>
224 </td><td><pre>
225 decorated = [ "(" + s + ")" | s <- undecorated ]
226 </pre>
227 or
228 <pre>
229 decorated = map (\s -> "(" + s + ")") undecorated
230 </pre>
231 </td></tr>
232
233 <tr><td><pre>
234 List<String> nonempty = new ArrayList<String>();
235 for(String s : l)
236     if(!s.isEmpty())
237         nonempty.add(l);
238 </pre>
239 </td><td><pre>
240 decorated = [ s | s <- l, s != "" ]
241 </pre>
242 or
243 <pre>
244 decorated = filter (\s -> s != "") l
245 </pre>
246 </td></tr>
247
248 <tr><td><pre>
249 List<List<String>> listOfLists;
250 List<String> flattened 
251     = new ArrayList<String>();
252 for(List<String> l : listOfLists)
253     for(String s : l)
254         flattened.add(s);
255 </pre>
256 </td><td><pre>
257 flattened = [ s | l <- listOfLists, s <- l ]
258 </pre>
259 or
260 <pre>
261 flattened = sum listOfLists
262 </pre>
263 or
264 <pre>
265 flattened = join listOfLists
266 </pre>
267 </td></tr>
268
269 <tr><td><pre>
270 throw new RuntimeException("Unhandled case.");
271 </pre>
272 </td><td>
273 <pre>fail "Unhandled case."</pre>
274 </td></tr>
275
276
277 <tr><td><pre>
278 <b>try</b> {
279     ...
280     <b>throw</b> new ExceptionX(...);
281     ...
282 } <b>catch</b>(ExceptionX e) {
283     ...
284 } <b>finally</b> {
285     ...
286 }
287 </pre>
288 </td><td>
289 Currently no correspondence. Exceptions need to be thrown and catched in Java code.
290 </td></tr>
291
292 <tr><td><pre>
293 <b>synchronized</b>(obj) {
294     ...
295 }
296 </pre>
297 </td><td>
298 Currently no correspondence. Synchronization must be done in Java code.
299 </td></tr>
300
301 <tr><td><pre>
302 <b>final static class</b> Vec3 {
303     public final double x;
304     public final double y;
305     public final double z;
306     
307     public Vec3(double x, 
308                 double y, 
309                 double x) {
310         this.x = x;
311         this.y = y;
312         this.z = z;
313     }
314 }
315 </pre>
316 </td><td><pre>
317 <b>data</b> Vec3 = Vec3 Double Double Double
318 </pre>
319
320 In future also:
321
322 <pre>
323 <b>data</b> Vec3 = Vec3 {
324   x :: Double,
325   y :: Double,
326   z :: Double
327 }
328 </pre>
329 </td></tr>
330
331 <tr><td><pre>
332 public static interface Num {}
333 public static class Int implements Num {
334     public static final int value;
335     public Int(int value) {
336         this.value = value;
337     }  
338 }
339 public static class Real implements Num {
340     public static final double value;
341     public Real(double value) {
342         this.value = value;
343     } 
344 }
345 </pre>
346 </td><td><pre>
347 data Num = Int Integer
348           | Real Double
349 </pre></td></tr>
350
351 <tr><td><pre>
352 public static Num add(Num a_, Num b_) {
353     if(a_ instanceof Int) {
354         Int a = (Int)a_;
355         if(b_ instanceof Int) {
356             Int b = (Int)b_;
357             return new Int(a.value + b.value);
358         }
359         else if(b_ instanceof Real) {
360             Real b = (Real)b_;
361             return new Real(a.value + b.value);
362         }
363     }
364     else if(a_ instanceof Real) {
365         Real a = (Real)a_;
366         if(b_ instanceof Int) {
367             Int b = (Int)b_;
368             return new Real(a.value + b.value);
369         }
370         else if(b_ instanceof Real) {
371             Real b = (Real)b_;
372             return new Real(a.value + b.value);
373         }
374     }
375     throw new IllegalArgumentException();
376 }
377 </pre>
378 </td><td><pre>
379 add :: Num -> Num -> Num
380 add (Int a)  (Int b)  = Int (a + b)
381 add (Int a)  (Real b) = Real (fromInteger a + b)
382 add (Real a) (Int b)  = Real (a + fromInteger b)
383 add (Real a) (Real b) = Real (a + b)
384 </pre></td></tr>
385
386 <tr><td><pre>
387 public static interface Bijection<A,B> {
388     B forward(A v);
389     A backward(B v);
390 }
391
392 Bijection<Integer,Integer> inc = 
393 new Bijection<Integer,Integer> {
394    public Integer forward(Integer v) {
395        return v + 1;
396    }
397    public Integer backward(Integer v) {
398        return v - 1;
399    }
400 }
401 </pre>
402 </td><td><pre>
403 data Bijection a b = 
404     Bijection (a -> b) (b -> a)
405     
406 forward (Bijection f _) v = f v 
407 backward (Bijection _ f) v = f v
408
409 inc = Bijection (\v -> v + 1) (\v -> v - 1)
410 </pre>
411
412 In future:
413
414 <pre>
415 data Bijection a b = Bijection {
416     forward :: a -> b,
417     backward :: b -> a
418 }
419
420 inc = Bijection {
421     forward v = v + 1,
422     backward v = v - 1
423 }
424 </pre>
425 </td></tr>
426 </table>
427
428 === Longer examples ===
429
430 <table border="1">
431 <tr><td><pre>
432 publid static int findIndex(List<String> strings, String target) {
433     int low=0, high=xs.size();
434     while(high-low > 1) {
435         int middle = (low + high)/2;
436         int cmp = xs.get(middle).compareTo(target);
437         if(cmp < 0)
438             low = middle;
439         else if(cmp > 0)
440             high = middle;
441         else
442             return middle;
443     }
444 }
445 </pre>
446 </td><td><pre>
447 findIndex :: [ String ] -> String -> Integer
448 findIndex strings target = loop 0 (length strings) 
449   where
450     loop low high 
451     | high - low > 1 = do
452         middle = (low + high) `div` 2
453         cmp = compare (xs!middle) target
454         if cmp < 0
455         then loop middle high
456         else if cmp > 0
457         then loop low middle
458         else middle
459     | otherwise = low 
460 </pre>
461 </td></tr>
462 </table>