Skip to content

Commit 022ac64

Browse files
Update scala-for-python-devs.md (#2895)
* 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.
1 parent 8c95e5e commit 022ac64

File tree

1 file changed

+55
-8
lines changed

1 file changed

+55
-8
lines changed

_overviews/scala3-book/scala-for-python-devs.md

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ At a high level, Scala shares these *similarities* with Python:
5050
Also at a high level, the _differences_ between Python and Scala are:
5151

5252
- 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`
5354
- Though it’s statically typed, Scala features like type inference make it feel like a dynamic language
5455
- Python is interpreted, and Scala code is compiled to _.class_ files, and runs on the Java Virtual Machine (JVM)
5556
- 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
6869
- The syntax for defining methods is similar
6970
- Both have lists, dictionaries (maps), sets, and tuples
7071
- Both have comprehensions for mapping and filtering
72+
- Both have terrific IDE support
7173
- With Scala 3’s [toplevel definitions][toplevel] you can put method, field, and other definitions anywhere
7274
- 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
7375

@@ -83,7 +85,6 @@ Also at a programming level, these are some of the differences you’ll see ever
8385
- The syntax that’s used to define functions that are passed into methods is the same syntax that’s used to define anonymous functions
8486
- Scala variables and parameters are defined with the `val` (immutable) or `var` (mutable) keywords
8587
- Scala idioms prefer immutable data structures
86-
- Scala has terrific IDE support with IntelliJ IDEA and Microsoft VS Code
8788
- Comments: Python uses `#` for comments; Scala uses the C, C++, and Java style: `//`, `/*...*/`, and `/**...*/`
8889
- Naming conventions: The Python standard is to use underscores like `my_list`; Scala uses `myList`
8990
- 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
9596
- Scala has state-of-the-art open source functional programming libraries (see the [“Awesome Scala” list](https://github.com/lauris/awesome-scala))
9697
- 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
9798
- 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
99100

100101
### Features compared with examples
101102

@@ -240,6 +241,53 @@ x += 1
240241

241242
However, the rule of thumb in Scala is to always use `val` unless the variable specifically needs to be mutated.
242243

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>&nbsp; name: str
259+
<br>&nbsp; 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&nbsp;&nbsp; # 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&nbsp;&nbsp; // Alice
285+
<br>val p2 = p.copy(age = 43)</code>
286+
</td>
287+
</tr>
288+
</tbody>
289+
</table>
290+
243291
## OOP style classes and methods
244292

245293
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
333381
## Interfaces, traits, and inheritance
334382

335383
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.
337385
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:
338386

339387
```scala
@@ -1010,7 +1058,7 @@ If you’re used to using these methods with lambda expressions in Python, you
10101058
To demonstrate this functionality, here are two sample lists:
10111059

10121060
```scala
1013-
numbers = (1,2,3) // python
1061+
numbers = [1,2,3] // python
10141062
val numbers = List(1,2,3) // scala
10151063
```
10161064

@@ -1110,7 +1158,8 @@ Those lists are used in the following table, that shows how to apply mapping and
11101158
### Scala collections methods
11111159

11121160
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.
11141163
In those method examples:
11151164

11161165
- `c` refers to a collection
@@ -1296,12 +1345,10 @@ Follow the links below for more details:
12961345

12971346
- Most concepts related to [contextual abstractions][contextual], such as [extension methods][extension-methods], [type classes][type-classes], implicit values
12981347
- 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
13001348
- The ability to create your own control structures and DSLs
1301-
- Pattern matching and `match` expressions
13021349
- [Multiversal equality][multiversal]: the ability to control at compile time what equality comparisons make sense
13031350
- Infix methods
1304-
- Macros and metaprogramming
1351+
- Macros
13051352

13061353
## Scala and virtual environments
13071354

0 commit comments

Comments
 (0)