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