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
* Update scala-for-python-devs.md
* Update scala-for-python-devs.md
Putting intersection types back in because Python has union types but not intersection types.
Copy file name to clipboardExpand all lines: _overviews/scala3-book/scala-for-python-devs.md
+55-8Lines changed: 55 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -50,6 +50,7 @@ At a high level, Scala shares these *similarities* with Python:
50
50
Also at a high level, the _differences_ between Python and Scala are:
51
51
52
52
- Python is dynamically typed, and Scala is statically typed
53
+
- Though it's dynamically typed, Python supports "gradual typing" with type hints, which are checked by static type checkers, like `mypy`
53
54
- Though it’s statically typed, Scala features like type inference make it feel like a dynamic language
54
55
- Python is interpreted, and Scala code is compiled to _.class_ files, and runs on the Java Virtual Machine (JVM)
55
56
- In addition to running on the JVM, the [Scala.js](https://www.scala-js.org) project lets you use Scala as a JavaScript replacement
@@ -68,6 +69,7 @@ This section looks at the similarities you’ll see between Python and Scala whe
68
69
- The syntax for defining methods is similar
69
70
- Both have lists, dictionaries (maps), sets, and tuples
70
71
- Both have comprehensions for mapping and filtering
72
+
- Both have terrific IDE support
71
73
- With Scala 3’s [toplevel definitions][toplevel] you can put method, field, and other definitions anywhere
72
74
- One difference is that Python can operate without even declaring a single method, while Scala 3 can’t do _everything_ at the toplevel; for instance, a [main method][main-method] (`@main def`) is required to start a Scala application
73
75
@@ -83,7 +85,6 @@ Also at a programming level, these are some of the differences you’ll see ever
83
85
- The syntax that’s used to define functions that are passed into methods is the same syntax that’s used to define anonymous functions
84
86
- Scala variables and parameters are defined with the `val` (immutable) or `var` (mutable) keywords
85
87
- Scala idioms prefer immutable data structures
86
-
- Scala has terrific IDE support with IntelliJ IDEA and Microsoft VS Code
87
88
- Comments: Python uses `#` for comments; Scala uses the C, C++, and Java style: `//`, `/*...*/`, and `/**...*/`
88
89
- Naming conventions: The Python standard is to use underscores like `my_list`; Scala uses `myList`
89
90
- Scala is statically typed, so you declare types for method parameters, method return values, and in other places
@@ -95,7 +96,7 @@ Also at a programming level, these are some of the differences you’ll see ever
95
96
- Scala has state-of-the-art open source functional programming libraries (see the [“Awesome Scala” list](https://github.com/lauris/awesome-scala))
96
97
- You can create your own “control structures” and DSLs, thanks to features like objects, by-name parameters, infix notation, optional parentheses, extension methods, higher-order functions, and more
97
98
- Scala code can run in the JVM and even be compiled to native images (using [Scala Native](https://github.com/scala-native/scala-native) and [GraalVM](https://www.graalvm.org)) for high performance
98
-
- Many other goodies: case classes, companion classes and objects, macros, [union][union-types] and [intersection][intersection-types] types, [toplevel definitions][toplevel], numeric literals, multiple parameter lists, and more
99
+
- Many other goodies: companion classes and objects, macros, numeric literals, multiple parameter lists, [intersection][intersection-types] types, type-level programming, and more
99
100
100
101
### Features compared with examples
101
102
@@ -240,6 +241,53 @@ x += 1
240
241
241
242
However, the rule of thumb in Scala is to always use `val` unless the variable specifically needs to be mutated.
242
243
244
+
## FP style records
245
+
246
+
Scala case classes are similar to Python frozen dataclasses.
247
+
248
+
### Constructor definition:
249
+
250
+
<table>
251
+
<tbody>
252
+
<tr>
253
+
<td class="python-block">
254
+
<code>from dataclasses import dataclass, replace
255
+
<br>
256
+
<br>@dataclass(frozen=True)
257
+
<br>class Person:
258
+
<br> name: str
259
+
<br> age: int</code>
260
+
</td>
261
+
</tr>
262
+
<tr>
263
+
<td class="scala-block">
264
+
<code>case class Person(name: String, age: Int)</code>
265
+
</td>
266
+
</tr>
267
+
</tbody>
268
+
</table>
269
+
270
+
### Create and use an instance:
271
+
272
+
<table>
273
+
<tbody>
274
+
<tr>
275
+
<td class="python-block">
276
+
<code>p = Person("Alice", 42)
277
+
<br>p.name # Alice
278
+
<br>p2 = replace(p, age=43)</code>
279
+
</td>
280
+
</tr>
281
+
<tr>
282
+
<td class="scala-block">
283
+
<code>val p = Person("Alice", 42)
284
+
<br>p.name // Alice
285
+
<br>val p2 = p.copy(age = 43)</code>
286
+
</td>
287
+
</tr>
288
+
</tbody>
289
+
</table>
290
+
243
291
## OOP style classes and methods
244
292
245
293
This section provides comparisons of features related to OOP-style classes and methods.
@@ -333,7 +381,7 @@ This section provides comparisons of features related to OOP-style classes and m
333
381
## Interfaces, traits, and inheritance
334
382
335
383
If you’re familiar with Java 8 and newer, Scala traits are similar to those Java interfaces.
336
-
Traits are used all the time in Scala, while Python interfaces and abstract classes are used much less often.
384
+
Traits are used all the time in Scala, while Python interfaces (Protocols) and abstract classes are used much less often.
337
385
Therefore, rather than attempt to compare the two, this example shows how to use Scala traits to build a small solution to a simulated math problem:
338
386
339
387
```scala
@@ -1010,7 +1058,7 @@ If you’re used to using these methods with lambda expressions in Python, you
1010
1058
To demonstrate this functionality, here are two sample lists:
1011
1059
1012
1060
```scala
1013
-
numbers =(1,2,3)// python
1061
+
numbers =[1,2,3]// python
1014
1062
valnumbers=List(1,2,3) // scala
1015
1063
```
1016
1064
@@ -1110,7 +1158,8 @@ Those lists are used in the following table, that shows how to apply mapping and
1110
1158
### Scala collections methods
1111
1159
1112
1160
Scala collections classes have over 100 functional methods to simplify your code.
1113
-
In addition to `map`, `filter`, and `reduce`, other commonly-used methods are listed below.
1161
+
In Python, some of these functions are available in the `itertools` module.
1162
+
In addition to `map`, `filter`, and `reduce`, other commonly-used methods in Scala are listed below.
1114
1163
In those method examples:
1115
1164
1116
1165
-`c` refers to a collection
@@ -1296,12 +1345,10 @@ Follow the links below for more details:
1296
1345
1297
1346
- Most concepts related to [contextual abstractions][contextual], such as [extension methods][extension-methods], [type classes][type-classes], implicit values
1298
1347
- Scala allows multiple parameter lists, which enables features like partially-applied functions, and the ability to create your own DSLs
1299
-
- Case classes, which are extremely useful for functional programming and pattern matching
1300
1348
- The ability to create your own control structures and DSLs
1301
-
- Pattern matching and `match` expressions
1302
1349
-[Multiversal equality][multiversal]: the ability to control at compile time what equality comparisons make sense
0 commit comments