From 628e93454aa7159bf73b961c9fafa9f36185deb5 Mon Sep 17 00:00:00 2001 From: Adit Sheth Date: Sun, 26 Jan 2025 20:42:41 -0800 Subject: [PATCH 1/3] Fixed bug 36921. --- docs/fsharp/language-reference/literals.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/docs/fsharp/language-reference/literals.md b/docs/fsharp/language-reference/literals.md index 8236b4543f4f8..9c76d67489abb 100644 --- a/docs/fsharp/language-reference/literals.md +++ b/docs/fsharp/language-reference/literals.md @@ -48,6 +48,13 @@ let x = "a" + "b" // evaluated at run-time let y = "a" + "b" // evaluated at compile-time ``` +> [!NOTE] +> Functions cannot be used to compute [] values because literals must be determined at compile-time and cannot depend on runtime evaluation. + +### Why functions cannot compute literals + +The `[]` attribute requires values to be known at compile-time. Functions, even if they seem to produce constant outputs, are evaluated at runtime, making them unsuitable for `[]`. This restriction ensures that literals can be safely used in scenarios like pattern matching, attribute arguments, and interop with external functions. + For example, this distinction matters when calling an [external function](functions/external-functions.md), because `DllImport` is an attribute that needs to know the value of `myDLL` during compilation. Without the `[]` declaration, this code would fail to compile: ```fsharp @@ -77,6 +84,20 @@ let Literal2 = 1 ||| 64 let Literal3 = System.IO.FileAccess.Read ||| System.IO.FileAccess.Write ``` +### Example of concise pattern matching using Named literals + +Named literals can make pattern matching more concise by avoiding the need for `when` clauses or additional logic. For example: + +```fsharp +[] +let ErrorCode = 404 + +let handleResponse code = + match code with + | ErrorCode -> "Not Found" + | _ -> "Other Response" +``` + ## Remarks Named literals are useful for: From 913cc3de2c0bcbb4e53f0a20f22178f5dbe50fb6 Mon Sep 17 00:00:00 2001 From: Adit Sheth Date: Mon, 27 Jan 2025 09:07:42 -0800 Subject: [PATCH 2/3] Resolved comments. --- docs/fsharp/language-reference/literals.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/docs/fsharp/language-reference/literals.md b/docs/fsharp/language-reference/literals.md index 9c76d67489abb..b634aa647f4e3 100644 --- a/docs/fsharp/language-reference/literals.md +++ b/docs/fsharp/language-reference/literals.md @@ -55,7 +55,14 @@ let y = "a" + "b" // evaluated at compile-time The `[]` attribute requires values to be known at compile-time. Functions, even if they seem to produce constant outputs, are evaluated at runtime, making them unsuitable for `[]`. This restriction ensures that literals can be safely used in scenarios like pattern matching, attribute arguments, and interop with external functions. -For example, this distinction matters when calling an [external function](functions/external-functions.md), because `DllImport` is an attribute that needs to know the value of `myDLL` during compilation. Without the `[]` declaration, this code would fail to compile: +For instance, attempting to assign the result of a function to a literal will fail: + +```fsharp +[] +let yFunc() = "a" + "b" // error FS0267: this is not a valid constant expression +``` + +This distinction also matters when calling an [external function](functions/external-functions.md). For example, `DllImport` is an attribute that needs to know the value of `myDLL` during compilation. Without the `[]` declaration, this code would fail to compile: ```fsharp [] From e082796c643f186a2acfb199d14b423d8caa692f Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 28 Jan 2025 07:23:30 -0500 Subject: [PATCH 3/3] Update docs/fsharp/language-reference/literals.md --- docs/fsharp/language-reference/literals.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/fsharp/language-reference/literals.md b/docs/fsharp/language-reference/literals.md index b634aa647f4e3..2e51f05b2d618 100644 --- a/docs/fsharp/language-reference/literals.md +++ b/docs/fsharp/language-reference/literals.md @@ -49,7 +49,7 @@ let y = "a" + "b" // evaluated at compile-time ``` > [!NOTE] -> Functions cannot be used to compute [] values because literals must be determined at compile-time and cannot depend on runtime evaluation. +> Functions cannot be used to compute `[]` values because literals must be determined at compile-time and cannot depend on runtime evaluation. ### Why functions cannot compute literals