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
{{ message }}
Repository navigation
Commit ce98866
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: csharp2fsharp.md
+6-13Lines changed: 6 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -84,11 +84,9 @@ Environment.Exit(exitCode)
84
84
* Operator `!=` becomes `<>`.
85
85
* Operator `!` becomes `not`.
86
86
* Operators `&&` and `||` are same in F#.
87
-
* No need to enclose entry point of program in Main() function, just write your statements
88
-
in a .fsx script or write the statements in the last `.fs` file fed to the F# compiler.
87
+
* No need to enclose entry point of program in Main() function, just write your statements in a .fsx script or write the statements in the last `.fs` file fed to the F# compiler.
89
88
90
-
In general, such a simple piece of code like the one in the example can be coded easily
91
-
without a mutable variable, just by doing readonly assignments, this way:
89
+
In general, such a simple piece of code like the one in the example can be coded easily without a mutable variable, just by doing readonly assignments, this way:
(NOTE: generictypesneedthequotecharacter (') as a prefix, as you might have noted above.)
136
133
@@ -168,11 +165,9 @@ finally
168
165
```
169
166
* The `catch` keyword becomes `with`.
170
167
* The `throw X;` clause becomes `raise X`, and an empty `throw;` becomes the function call `reraise()`.
171
-
* However, there are no `try-with-finally` blocks! We have only `try-with` blocks and `try-finally` blocks. Therefore
172
-
the equivalent in F# would need nesting (like it's done in the example above).
168
+
* However, there are no `try-with-finally` blocks! We have only `try-with` blocks and `try-finally` blocks. Therefore the equivalent in F# would need nesting (like it's done in the example above).
173
169
174
-
You may think this is an F# downside but try-catch-finally blocks are extremely
175
-
rare, especially given the `using` construct (for `IDisposable`) in C#:
170
+
You may think this is an F# downside but try-catch-finally blocks are extremely rare, especially given the `using` construct (for `IDisposable`) in C#:
176
171
177
172
```csharp
178
173
using (var reader = new StreamReader(someFile))
@@ -217,9 +212,7 @@ let Check(someParam1: Option<SomeType>, someParam2: Option<SomeType>): unit =
217
212
| _ -> ()
218
213
219
214
```
220
-
In C# you write null checks everywhere (no safety at compile time). In F#,
221
-
you do the null check in a safer way with an `Option<T>` type (similar to
222
-
`Nullable<T>` but better) and a match expression (pattern matching).
215
+
In C# you write null checks everywhere (no safety at compile time). In F#, you do the null check in a safer way with an `Option<T>` type (similar to `Nullable<T>` but better) and a match expression (pattern matching).
223
216
224
217
* When you don't want to return anything, in C# you use `void` which is metadata for specifying absence of a type, but in F# you need to return a special type called `unit`, which only has one possible value: `()`. That's why generally `()` means doing nothing (as per the above code).
225
218
* A `match-with` block is almost like a switch block, but more succint because it includes the casting (to someValue).
* Commas become semicolons when declaring elements of an array/list/dictionary.
113
-
* You use `dict` to initialize an `IDictionary<string,int>` collection, however in F#
114
-
you would rather use a `Map<string,int>` because the latter is immutable.
113
+
* You use `dict` to initialize an `IDictionary<string,int>` collection, however in F# you would rather use a `Map<string,int>` because the latter is immutable.
115
114
116
-
NOTE: if you were to create a collection type whose elements had a type that is not
117
-
defined yet (it could change at runtime), then you have to use the quote character
118
-
to denote those generic types. Example: instead of `Map<string,int>`, `Map<'K,'V>`,
119
-
meaning `'K` is the key's type and `'V` is the value's type.
115
+
NOTE: if you were to create a collection type whose elements had a type that is not defined yet (it could change at runtime), then you have to use the quote character to denote those generic types. Example: instead of `Map<string,int>`, `Map<'K,'V>`, meaning `'K` is the key's type and `'V` is the value's type.
120
116
121
117
122
118
### Example 4: Basic blocks
@@ -151,11 +147,9 @@ finally
151
147
```
152
148
* The `except` keyword becomes `with`.
153
149
* The `raise X` is same in F#, but an empty `raise` becomes the function call `reraise()`.
154
-
* However, there are no `try-with-finally` blocks! We have only `try-with` blocks and `try-finally` blocks. Therefore
155
-
the equivalent in F# would need nesting (like it's done in the example above).
150
+
* However, there are no `try-with-finally` blocks! We have only `try-with` blocks and `try-finally` blocks. Therefore the equivalent in F# would need nesting (like it's done in the example above).
156
151
157
-
You may think this is an F# downside but try-catch-finally blocks are extremely
158
-
rare, especially given the `with` construct (for `Disposable` objects) in Python:
152
+
You may think this is an F# downside but try-catch-finally blocks are extremely rare, especially given the `with` construct (for `Disposable` objects) in Python:
159
153
160
154
```python
161
155
with Reader() as reader:
@@ -196,9 +190,7 @@ let Check(someParam1: Option<SomeType>, someParam2: Option<SomeType>): unit =
196
190
| _ -> ()
197
191
198
192
```
199
-
In python you write None checks everywhere (no safety at compile time). In F#,
200
-
you do the null check in a safer way with an `Option<T>` type and a match
201
-
expression (pattern matching).
193
+
In python you write None checks everywhere (no safety at compile time). In F#, you do the null check in a safer way with an `Option<T>` type and a match expression (pattern matching).
202
194
203
195
* When you don't want to return anything, in python you just don't use the `return` statement, but in F# you need to return a special type called `unit`, which only has one possible value: `()`. That's why generally `()` means doing nothing (as per the above code).
204
196
* A `match-with` block is almost like a switch block, but more succint because it includes the casting (to someValue).
0 commit comments