forked from leverich/swiftislikescala
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
595 lines (580 loc) · 22.1 KB
/
index.html
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
<!DOCTYPE html>
<!-- Please edit the index.jade or the files in comparisons/, this file is generated--><!--[if lt IE 10 ]> <html class="old-ie"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<title>Swift is a lot like Scala</title>
<meta name="description" content="Examples of how Apple's Swift language is similar to Scala.">
<link rel="stylesheet" href="css/prism.css">
<link rel="stylesheet" href="css/index.css">
<script type="text/javascript" src="//use.typekit.net/ghy0wve.js"></script>
<script type="text/javascript">try{Typekit.load();}catch(e){}</script>
<script src="js/prism.js"></script>
<script src="js/index.js"></script>
</head>
<body><a href="https://github.com/leverich/swiftislikescala"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://camo.githubusercontent.com/a6677b08c955af8400f44c6298f40e7d19cc5b2d/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f677261795f3664366436642e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_gray_6d6d6d.png"></a>
<div class="page">
<header class="page-header">
<div class="inner">
<h1>Swift is a lot like Scala</h1>
</div>
</header>
<div class="explanation">
<div class="inner">
<p>A new programming language called <a href="https://developer.apple.com/swift/">Swift</a> was announced at WWDC'14. Here's a source-to-source comparison of Swift and <a href="http://www.scala-lang.org/">Scala</a> using the <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GuidedTour.html">examples</a> given in "The Swift Programming Language" book published by Apple in the iTunes Store.</p>
<p>I suspect the two are related ;-) Syntacticly, it's fair to say that Swift is a dialect of Scala. Swift inherits from Scala most of the "banner" features listed by Apple: Type Inference, Closures, Tuples, Generics, Read-Eval-Print-Loop, etc.</p>
<p>
Despite their syntactic similarity, Swift's runtime environment is quite distinct from Scala's, and is perhaps the most interesting aspect of the new language.
Scala compiles to the JVM, uses garbage collection, and its object model transparently integrates with Java.
Swift compiles to native code, uses automatic reference counting, and its object model transparently integrates with Objective-C.
Thus, the similarity between the two languages does not extend much below the surface.
</p>
</div>
</div>
<header class="search">
<input type="search" placeholder="Search..." required>
</header>
<div class="comparisons">
<div class="empty-message">Your search didn't match any comparisons.</div>
<section id="basics" class="odd">
<div class="inner">
<h2>Basics</h2>
<div id="hello_world" class="comparison">
<h3><a href="#hello_world">Hello World</a></h3>
<div data-browser="swift" class="browser swift">
<h4>SWIFT</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>println("Hello, world!")
</code></pre>
</div>
</div>
<div data-browser="scala" class="browser scala">
<h4>SCALA</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>println("Hello, world!")
</code></pre>
</div>
</div>
</div>
<div id="variables_and_constants" class="comparison">
<h3><a href="#variables_and_constants">Variables And Constants</a></h3>
<div data-browser="swift" class="browser swift">
<h4>SWIFT</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>var myVariable = 42
myVariable = 50
let myConstant = 42
</code></pre>
</div>
</div>
<div data-browser="scala" class="browser scala">
<h4>SCALA</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>var myVariable = 42
myVariable = 50
val myConstant = 42
</code></pre>
</div>
</div>
</div>
<div id="explicit_types" class="comparison">
<h3><a href="#explicit_types">Explicit Types</a></h3>
<div data-browser="swift" class="browser swift">
<h4>SWIFT</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>let explicitDouble: Double = 70
</code></pre>
</div>
</div>
<div data-browser="scala" class="browser scala">
<h4>SCALA</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>val explicitDouble: Double = 70
</code></pre>
</div>
</div>
</div>
<div id="type_coercion" class="comparison">
<h3><a href="#type_coercion">Type Coercion</a></h3>
<div data-browser="swift" class="browser swift">
<h4>SWIFT</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>let label = "The width is "
let width = 94
let widthLabel = label + String(width)
</code></pre>
</div>
</div>
<div data-browser="scala" class="browser scala">
<h4>SCALA</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>val label = "The width is "
val width = 94
val widthLabel = label + width
</code></pre>
</div>
</div>
</div>
<div id="string_interpolation" class="comparison">
<h3><a href="#string_interpolation">String Interpolation</a></h3>
<div data-browser="swift" class="browser swift">
<h4>SWIFT</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>let apples = 3
let oranges = 5
let fruitSummary = "I have \(apples + oranges) " +
"pieces of fruit."
</code></pre>
</div>
</div>
<div data-browser="scala" class="browser scala">
<h4>SCALA</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>val apples = 3
val oranges = 5
val fruitSummary = s"I have ${apples + oranges} " +
" pieces of fruit."
</code></pre>
</div>
</div>
</div>
<div id="range_operator" class="comparison">
<h3><a href="#range_operator">Range Operator</a></h3>
<div data-browser="swift" class="browser swift">
<h4>SWIFT</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>let names = ["Anna", "Alex", "Brian", "Jack"]
let count = names.count
for i in 0..count {
println("Person \(i + 1) is called \(names[i])")
}
// Person 1 is called Anna
// Person 2 is called Alex
// Person 3 is called Brian
// Person 4 is called Jack
</code></pre>
</div>
</div>
<div data-browser="scala" class="browser scala">
<h4>SCALA</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>val names = Array("Anna", "Alex", "Brian", "Jack")
val count = names.length
for (i <- 0 until count) {
println(s"Person ${i + 1} is called ${names(i)}")
}
// Person 1 is called Anna
// Person 2 is called Alex
// Person 3 is called Brian
// Person 4 is called Jack
</code></pre>
</div>
</div>
</div>
<div id="inclusive_range_operator" class="comparison">
<h3><a href="#inclusive_range_operator">Inclusive Range Operator</a></h3>
<div data-browser="swift" class="browser swift">
<h4>SWIFT</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>for index in 1...5 {
println("\(index) times 5 is \(index * 5)")
}
// 1 times 5 is 5
// 2 times 5 is 10
// 3 times 5 is 15
// 4 times 5 is 20
// 5 times 5 is 25
</code></pre>
</div>
</div>
<div data-browser="scala" class="browser scala">
<h4>SCALA</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>for (index <- 1 to 5) {
println(s"$index times 5 is ${index * 5}")
}
// 1 times 5 is 5
// 2 times 5 is 10
// 3 times 5 is 15
// 4 times 5 is 20
// 5 times 5 is 25
</code></pre>
</div>
</div>
</div>
</div>
</section>
<section id="collections">
<div class="inner">
<h2>Collections</h2>
<div id="arrays" class="comparison">
<h3><a href="#arrays">Arrays</a></h3>
<div data-browser="swift" class="browser swift">
<h4>SWIFT</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>var shoppingList = ["catfish", "water",
"tulips", "blue paint"]
shoppingList[1] = "bottle of water"
</code></pre>
</div>
</div>
<div data-browser="scala" class="browser scala">
<h4>SCALA</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>var shoppingList = Array("catfish",
"water", "tulips", "blue paint")
shoppingList(1) = "bottle of water"
</code></pre>
</div>
</div>
</div>
<div id="maps" class="comparison">
<h3><a href="#maps">Maps</a></h3>
<div data-browser="swift" class="browser swift">
<h4>SWIFT</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>var occupations = [
"Malcolm": "Captain",
"Kaylee": "Mechanic",
]
occupations["Jayne"] = "Public Relations"
</code></pre>
</div>
</div>
<div data-browser="scala" class="browser scala">
<h4>SCALA</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>var occupations = scala.collection.mutable.Map(
"Malcolm" -> "Captain",
"Kaylee" -> "Mechanic"
)
occupations("Jayne") = "Public Relations"
</code></pre>
</div>
</div>
</div>
<div id="empty_collections" class="comparison">
<h3><a href="#empty_collections">Empty Collections</a></h3>
<div data-browser="swift" class="browser swift">
<h4>SWIFT</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>let emptyArray = String[]()
let emptyDictionary = Dictionary<String, Float>()
let emptyArrayNoType = []
</code></pre>
</div>
</div>
<div data-browser="scala" class="browser scala">
<h4>SCALA</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>val emptyArray = Array[String]()
val emptyDictionary = Map[String, Float]()
val emptyArrayNoType = Array()
</code></pre>
</div>
</div>
</div>
</div>
</section>
<section id="functions" class="odd">
<div class="inner">
<h2>Functions</h2>
<div id="functions" class="comparison">
<h3><a href="#functions">Functions</a></h3>
<div data-browser="swift" class="browser swift">
<h4>SWIFT</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>func greet(name: String, day: String) -> String {
return "Hello \(name), today is \(day)."
}
greet("Bob", "Tuesday")
</code></pre>
</div>
</div>
<div data-browser="scala" class="browser scala">
<h4>SCALA</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>def greet(name: String, day: String): String = {
return s"Hello $name, today is $day."
}
greet("Bob", "Tuesday")
</code></pre>
</div>
</div>
</div>
<div id="tuple_return" class="comparison">
<h3><a href="#tuple_return">Tuple Return</a></h3>
<div data-browser="swift" class="browser swift">
<h4>SWIFT</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>func getGasPrices() -> (Double, Double, Double) {
return (3.59, 3.69, 3.79)
}
</code></pre>
</div>
</div>
<div data-browser="scala" class="browser scala">
<h4>SCALA</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>def getGasPrices(): (Double, Double, Double) = {
return (3.59, 3.69, 3.79)
}
</code></pre>
</div>
</div>
</div>
<div id="variable_number_of_arguments" class="comparison">
<h3><a href="#variable_number_of_arguments">Variable Number Of Arguments</a></h3>
<div data-browser="swift" class="browser swift">
<h4>SWIFT</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>func sumOf(numbers: Int...) -> Int {
var sum = 0
for number in numbers {
sum += number
}
return sum
}
sumOf(42, 597, 12)
</code></pre>
</div>
</div>
<div data-browser="scala" class="browser scala">
<h4>SCALA</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>def sumOf(numbers: Int*): Int = {
var sum = 0
for (number <- numbers) {
sum += number
}
return sum
}
sumOf(42, 597, 12)
</code></pre>
</div>
</div>
</div>
<div id="function_type" class="comparison">
<h3><a href="#function_type">Function Type</a></h3>
<div data-browser="swift" class="browser swift">
<h4>SWIFT</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>func makeIncrementer() -> (Int -> Int) {
func addOne(number: Int) -> Int {
return 1 + number
}
return addOne
}
var increment = makeIncrementer()
increment(7)
</code></pre>
</div>
</div>
<div data-browser="scala" class="browser scala">
<h4>SCALA</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>def makeIncrementer(): Int => Int = {
def addOne(number: Int): Int = {
return 1 + number
}
return addOne
}
var increment = makeIncrementer()
increment(7)
</code></pre>
</div>
</div>
</div>
<div id="map" class="comparison">
<h3><a href="#map">Map</a></h3>
<div data-browser="swift" class="browser swift">
<h4>SWIFT</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>var numbers = [20, 19, 7, 12]
numbers.map({ number in 3 * number })
</code></pre>
</div>
</div>
<div data-browser="scala" class="browser scala">
<h4>SCALA</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>var numbers = Array(20, 19, 7, 12)
numbers.map({ number => 3 * number })
</code></pre>
</div>
</div>
</div>
<div id="sort" class="comparison">
<h3><a href="#sort">Sort</a></h3>
<div data-browser="swift" class="browser swift">
<h4>SWIFT</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>sort([1, 5, 3, 12, 2]) { $0 > $1 }
</code></pre>
</div>
</div>
<div data-browser="scala" class="browser scala">
<h4>SCALA</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>Array(1, 5, 3, 12, 2).sortWith(_ > _)
</code></pre>
</div>
</div>
</div>
<div id="named_arguments" class="comparison">
<h3><a href="#named_arguments">Named Arguments</a></h3>
<div data-browser="swift" class="browser swift">
<h4>SWIFT</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>def area(width: Int, height: Int) -> Int {
return width * height
}
area(width: 10, height: 10)
</code></pre>
</div>
</div>
<div data-browser="scala" class="browser scala">
<h4>SCALA</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>def area(width: Int, height: Int): Int = {
return width * height
}
area(width = 10, height = 10)
</code></pre>
</div>
</div>
</div>
</div>
</section>
<section id="classes">
<div class="inner">
<h2>Classes</h2>
<div id="declaration" class="comparison">
<h3><a href="#declaration">Declaration</a></h3>
<div data-browser="swift" class="browser swift">
<h4>SWIFT</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>class Shape {
var numberOfSides = 0
func simpleDescription() -> String {
return "A shape with \(numberOfSides) sides."
}
}
</code></pre>
</div>
</div>
<div data-browser="scala" class="browser scala">
<h4>SCALA</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>class Shape {
var numberOfSides = 0
def simpleDescription(): String = {
return s"A shape with $numberOfSides sides."
}
}
</code></pre>
</div>
</div>
</div>
<div id="usage" class="comparison">
<h3><a href="#usage">Usage</a></h3>
<div data-browser="swift" class="browser swift">
<h4>SWIFT</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>var shape = Shape()
shape.numberOfSides = 7
var shapeDescription = shape.simpleDescription()
</code></pre>
</div>
</div>
<div data-browser="scala" class="browser scala">
<h4>SCALA</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>var shape = new Shape()
shape.numberOfSides = 7
var shapeDescription = shape.simpleDescription()
</code></pre>
</div>
</div>
</div>
<div id="subclass" class="comparison">
<h3><a href="#subclass">Subclass</a></h3>
<div data-browser="swift" class="browser swift">
<h4>SWIFT</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>class NamedShape {
var numberOfSides: Int = 0
var name: String
init(name: String) {
self.name = name
}
func simpleDescription() -> String {
return "A shape with \(numberOfSides) sides."
}
}
class Square: NamedShape {
var sideLength: Double
init(sideLength: Double, name: String) {
self.sideLength = sideLength
super.init(name: name)
numberOfSides = 4
}
func area() -> Double {
return sideLength * sideLength
}
override func simpleDescription() -> String {
return "A square with sides of length \(sideLength)."
}
}
let test = Square(sideLength: 5.2)
test.area()
test.simpleDescription()
</code></pre>
</div>
</div>
<div data-browser="scala" class="browser scala">
<h4>SCALA</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>class NamedShape(var name: String) {
var numberOfSides: Int = 0
def simpleDescription(): String = {
return s"A shape with $numberOfSides sides."
}
}
class Square(var sideLength: Double, name: String)
extends NamedShape(name) {
def area(): Double = {
return sideLength * sideLength
}
def Square(sideLength: Double, name: String) = {
this.sideLength = sideLength
numberOfSides = 4
}
override def simpleDescription(): String = {
return s"A square with sides of length $sideLength."
}
}
val test = new Square(5.2, "my test square")
test.area()
test.simpleDescription()
</code></pre>
</div>
</div>
</div>
</div>
</section>
</div>
<footer><p>Shamelessly forked from <a href="http://youmightnotneedjquery.com/">http://youmightnotneedjquery.com/</a>.</footer>
</div>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-20537147-2', 'leverich.github.io');
ga('send', 'pageview');
</script>
<div style="-webkit-transform: translateZ(0);"></div>
</body>