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
Copy file name to clipboardExpand all lines: docs/fsharp/get-started/get-started-visual-studio.md
+4-4
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,12 @@
1
1
---
2
2
title: Get started with F# in Visual Studio
3
3
description: Learn how to use F# with Visual Studio.
4
-
ms.date: 12/20/2019
4
+
ms.date: 10/29/2021
5
5
recommendations: false
6
6
---
7
7
# Get started with F# in Visual Studio
8
8
9
-
F# and the Visual F# tooling are supported in the Visual Studio integrated development environment (IDE).
9
+
F# is supported in the Visual Studio integrated development environment (IDE).
10
10
11
11
To begin, ensure that you have [Visual Studio installed with F# support](install-fsharp.md#install-f-with-visual-studio).
12
12
@@ -35,10 +35,10 @@ Let's get started by writing some code. Make sure that the `Program.fs` file is
35
35
The previous code sample defines a function called `square` that takes an input named `x` and multiplies it by itself. Because F# uses [Type inference](../language-reference/type-inference.md), the type of `x` doesn't need to be specified. The F# compiler understands the types where multiplication is valid and assigns a type to `x` based on how `square` is called. If you hover over `square`, you should see the following:
36
36
37
37
```fsharp
38
-
val square: x:int -> int
38
+
val square: x:int -> int
39
39
```
40
40
41
-
This is what is known as the function's type signature. It can be read like this: "Square is a function that takes an integer named x and produces an integer". The compiler gave `square` the `int` type for now; this is because multiplication is not generic across *all* types but rather a closed set of types. The F# compiler will adjust the type signature if you call `square` with a different input type, such as a `float`.
41
+
This is what is known as the function's type signature. It can be read like this: "Square is a function that takes an integer named x and produces an integer". The compiler gave `square` the `int` type for now.
42
42
43
43
Another function, `main`, is defined, which is decorated with the `EntryPoint` attribute. This attribute tells the F# compiler that program execution should start there. It follows the same convention as other [C-style programming languages](https://en.wikipedia.org/wiki/Entry_point#C_and_C.2B.2B), where command-line arguments can be passed to this function, and an integer code is returned (typically `0`).
Copy file name to clipboardExpand all lines: docs/fsharp/get-started/get-started-vscode.md
+8-8
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
title: Get Started with F# in Visual Studio Code
3
3
description: Learn how to use F# with Visual Studio Code and the Ionide plugin suite.
4
-
ms.date: 07/23/2021
4
+
ms.date: 10/29/2021
5
5
---
6
6
# Get Started with F# in Visual Studio Code
7
7
@@ -55,7 +55,7 @@ toPigLatin "banana";;
55
55
You should see the following result:
56
56
57
57
```fsharp
58
-
val it: string = "ananabay"
58
+
val it: string = "ananabay"
59
59
```
60
60
61
61
Now, let's try with a vowel as the first letter. Enter the following:
@@ -67,7 +67,7 @@ toPigLatin "apple";;
67
67
You should see the following result:
68
68
69
69
```fsharp
70
-
val it: string = "appleyay"
70
+
val it: string = "appleyay"
71
71
```
72
72
73
73
The function appears to be working as expected. Congratulations, you just wrote your first F# function in Visual Studio Code and evaluated it with FSI!
@@ -86,7 +86,7 @@ If the first character in a word starts with a vowel, add "yay" to the end of th
86
86
You may have noticed the following in FSI:
87
87
88
88
```fsharp
89
-
val toPigLatin: word:string -> string
89
+
val toPigLatin: word:string -> string
90
90
```
91
91
92
92
This states that `toPigLatin` is a function that takes in a `string` as input (called `word`), and returns another `string`. This is known as the [type signature of the function](https://fsharpforfunandprofit.com/posts/function-signatures/), a fundamental piece of F# that's key to understanding F# code. You'll also notice this if you hover over the function in Visual Studio Code.
@@ -125,10 +125,10 @@ Now, in the `main` function, call your Pig Latin generator function on the argum
Copy file name to clipboardExpand all lines: docs/fsharp/get-started/get-started-with-visual-studio-for-mac.md
+11-11
Original file line number
Diff line number
Diff line change
@@ -1,11 +1,11 @@
1
1
---
2
2
title: Get started with F# in Visual Studio for Mac
3
3
description: Learn how to use F# with Visual Studio for Mac.
4
-
ms.date: 07/03/2018
4
+
ms.date: 10/29/2021
5
5
---
6
6
# Get started with F# in Visual Studio for Mac
7
7
8
-
F# and the Visual F# tooling are supported in the Visual Studio for Mac IDE. Ensure that you have [Visual Studio for Mac installed](install-fsharp.md#install-f-with-visual-studio-for-mac).
8
+
F# is supported in the Visual Studio for Mac IDE. Ensure that you have [Visual Studio for Mac installed](install-fsharp.md#install-f-with-visual-studio-for-mac).
9
9
10
10
## Creating a console application
11
11
@@ -53,14 +53,14 @@ Congratulations! You've created your first F# project in Visual Studio for Mac,
53
53
54
54
## Using F# Interactive
55
55
56
-
One of the best features of the Visual F# tooling in Visual Studio for Mac is the F# Interactive Window. It allows you to send code over to a process where you can call that code and see the result interactively.
56
+
One of the best features of F# tooling in Visual Studio for Mac is the F# Interactive Window. It allows you to send code over to a process where you can call that code and see the result interactively.
57
57
58
58
To begin using it, highlight the `square` function defined in your code. Next, click on **Edit** from the top level menu. Next select **Send selection to F# Interactive**. This executes the code in the F# Interactive Window. Alternatively, you can right click on the selection and choose **Send selection to F# Interactive**. You should see the F# Interactive Window appear with the following in it:
59
59
60
60
```console
61
61
>
62
62
63
-
val square: x:int -> int
63
+
val square: x:int -> int
64
64
65
65
>
66
66
```
@@ -69,34 +69,34 @@ This shows the same function signature for the `square` function, which you saw
69
69
70
70
```console
71
71
> square 12;;
72
-
val it: int = 144
73
-
>square 13;;
74
-
val it: int = 169
72
+
val it: int = 144
73
+
>square 13;;
74
+
val it: int = 169
75
75
```
76
76
77
77
This executes the function, binds the result to a new name `it`, and displays the type and value of `it`. Note that you must terminate each line with `;;`. This is how F# Interactive knows when your function call is finished. You can also define new functions in F# Interactive:
78
78
79
79
```console
80
80
> let isOdd x = x % 2 <> 0;;
81
81
82
-
val isOdd: x:int -> bool
82
+
val isOdd: x:int -> bool
83
83
84
84
> isOdd 12;;
85
-
val it: bool = false
85
+
val it: bool = false
86
86
```
87
87
88
88
The above defines a new function, `isOdd`, which takes an `int` and checks to see if it's odd! You can call this function to see what it returns with different inputs. You can call functions within function calls:
89
89
90
90
```console
91
91
> isOdd (square 15);;
92
-
val it: bool = true
92
+
val it: bool = true
93
93
```
94
94
95
95
You can also use the [pipe-forward operator](../language-reference/symbol-and-operator-reference/index.md) to pipeline the value into the two functions:
96
96
97
97
```console
98
98
> 15 |> square |> isOdd;;
99
-
val it: bool = true
99
+
val it: bool = true
100
100
```
101
101
102
102
The pipe-forward operator, and more, are covered in later tutorials.
## Struct Representations for Partial Active Patterns
136
+
137
+
By default, partial active patterns return an `option` value, which will involve an allocation for the `Some` value on a successful match. Alternatively, you can use a [value option](value-options.md) as a return value through the use of the `Struct` attribute:
138
+
139
+
```fsharp
140
+
open System
141
+
142
+
[<return: Struct>]
143
+
let (|Int|_|) str =
144
+
match Int32.TryParse(str) with
145
+
| (true, n) -> ValueSome n
146
+
| _ -> ValueNone
147
+
```
148
+
149
+
The attribute must be specified, because the use of a struct return is not inferred from simply changing the return type to `ValueOption`. For more information, see [RFC FS-1039](https://github.com/fsharp/fslang-design/blob/main/FSharp-6.0/FS-1039-struct-representation-for-active-patterns.md).
0 commit comments