From 2ef133761d89ea90f07f9bf680dcf71775147360 Mon Sep 17 00:00:00 2001 From: soroshsabz Date: Tue, 28 Mar 2017 09:40:12 +0430 Subject: [PATCH 01/11] Create readonly-locals-and-parameters.md Initial commit. --- proposals/readonly-locals-and-parameters.md | 39 +++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 proposals/readonly-locals-and-parameters.md diff --git a/proposals/readonly-locals-and-parameters.md b/proposals/readonly-locals-and-parameters.md new file mode 100644 index 0000000000..f49b1a5892 --- /dev/null +++ b/proposals/readonly-locals-and-parameters.md @@ -0,0 +1,39 @@ +In the name of Allah + +# Readonly locals and parameters + +* [x] Proposed +* [ ] Prototype: Not Started +* [ ] Implementation: Not Started +* [ ] Specification: Not Started + +## Summary +[summary]: #summary + +The "readonly locals and parameters" feature is actually a group or features that declare local variables and method parameters without exposing the state to modifications. + +# Prevoius work + +There is an existing proposal that touches this topic https://github.com/dotnet/roslyn/issues/115 as a special case of readonly parameters without going into many details. +Here I just want to acknowledge that the idea by itself is not new anyway. + +## Motivation + +## Solution + +## Syntax + +## Drawbacks +[drawbacks]: #drawbacks + +## Alternatives +[alternatives]: #alternatives + +The main competing design is really "do nothing". + +## Unresolved questions +[unresolved]: #unresolved-questions + +## Design meetings + +Link to design notes that affect this proposal, and describe in one sentence for each what changes they led to. From e92137733fa21a20dce3e4c5afb98ad1591a40cd Mon Sep 17 00:00:00 2001 From: soroshsabz Date: Tue, 28 Mar 2017 09:41:00 +0430 Subject: [PATCH 02/11] Update readonly-locals-and-parameters.md --- proposals/readonly-locals-and-parameters.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/proposals/readonly-locals-and-parameters.md b/proposals/readonly-locals-and-parameters.md index f49b1a5892..db6839905b 100644 --- a/proposals/readonly-locals-and-parameters.md +++ b/proposals/readonly-locals-and-parameters.md @@ -14,7 +14,7 @@ The "readonly locals and parameters" feature is actually a group or features tha # Prevoius work -There is an existing proposal that touches this topic https://github.com/dotnet/roslyn/issues/115 as a special case of readonly parameters without going into many details. +There is an existing proposal that touches this topic https://github.com/dotnet/roslyn/issues/115. Here I just want to acknowledge that the idea by itself is not new anyway. ## Motivation From 6dc873a4035743a517e837aa27801fc3b601ead4 Mon Sep 17 00:00:00 2001 From: soroshsabz Date: Tue, 28 Mar 2017 10:46:45 +0430 Subject: [PATCH 03/11] Update readonly-locals-and-parameters.md Add motivation description. --- proposals/readonly-locals-and-parameters.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/proposals/readonly-locals-and-parameters.md b/proposals/readonly-locals-and-parameters.md index db6839905b..7c0860413c 100644 --- a/proposals/readonly-locals-and-parameters.md +++ b/proposals/readonly-locals-and-parameters.md @@ -14,11 +14,21 @@ The "readonly locals and parameters" feature is actually a group or features tha # Prevoius work -There is an existing proposal that touches this topic https://github.com/dotnet/roslyn/issues/115. +There is an existing proposal that touches this topics https://github.com/dotnet/roslyn/issues/115 and #188. Here I just want to acknowledge that the idea by itself is not new anyway. ## Motivation +Maximizing data and state immutability in any program has value that cause to improve readability, predictability and maintainability. So many languages have some concepts and features to provide some facilities about it and because above benefits these features are many uses in programs and master programmers recommended to use them, for example [Scott Meyers in Effective C++ book third edition](https://en.wikipedia.org/wiki/Scott_Meyers) says: +> Use const whenever possible + +And when he wants to describe it says: + +>The wonderful thing about const is that it allows you to specify a semantic constraint — a particular object should not be modified — and compilers will enforce that constraint. It allows you to communicate to both compilers and other programmers that a value should remain invariant. Whenever that is true, you should be sure to say so, because that way you enlist your compilers’ aid in making sure the +constraint isn’t violated. + +In this scope C# has some features like readonly/const member, but has many missing features to help programmer to gain maximum immutability in program easily. One of these missing features has ability to declare local variables and method parameters without permit any modification after initialization. + ## Solution ## Syntax From ca1edb9181dc5dc474facede46b6f93d7cf4f14a Mon Sep 17 00:00:00 2001 From: soroshsabz Date: Tue, 28 Mar 2017 10:48:51 +0430 Subject: [PATCH 04/11] Update readonly-locals-and-parameters.md Corrupted link repaired. --- proposals/readonly-locals-and-parameters.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/proposals/readonly-locals-and-parameters.md b/proposals/readonly-locals-and-parameters.md index 7c0860413c..9eaf89dae6 100644 --- a/proposals/readonly-locals-and-parameters.md +++ b/proposals/readonly-locals-and-parameters.md @@ -14,7 +14,7 @@ The "readonly locals and parameters" feature is actually a group or features tha # Prevoius work -There is an existing proposal that touches this topics https://github.com/dotnet/roslyn/issues/115 and #188. +There is an existing proposal that touches this topics https://github.com/dotnet/roslyn/issues/115 and https://github.com/dotnet/csharplang/issues/188. Here I just want to acknowledge that the idea by itself is not new anyway. ## Motivation From b48c4eadb52e55e12dbf17da5f4339dbbd5c447e Mon Sep 17 00:00:00 2001 From: soroshsabz Date: Tue, 28 Mar 2017 10:51:16 +0430 Subject: [PATCH 05/11] Update readonly-locals-and-parameters.md --- proposals/readonly-locals-and-parameters.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/proposals/readonly-locals-and-parameters.md b/proposals/readonly-locals-and-parameters.md index 9eaf89dae6..26a2bea88f 100644 --- a/proposals/readonly-locals-and-parameters.md +++ b/proposals/readonly-locals-and-parameters.md @@ -10,7 +10,7 @@ In the name of Allah ## Summary [summary]: #summary -The "readonly locals and parameters" feature is actually a group or features that declare local variables and method parameters without exposing the state to modifications. +The "readonly locals and parameters" feature is actually a group or features that declare local variables and method parameters without permit the state (objects or primitives) to modifications. # Prevoius work From 80161de1ac239449469624b3b05b6d53b3236480 Mon Sep 17 00:00:00 2001 From: soroshsabz Date: Tue, 28 Mar 2017 12:03:11 +0430 Subject: [PATCH 06/11] Update readonly-locals-and-parameters.md Add local readonly syntax --- proposals/readonly-locals-and-parameters.md | 33 +++++++++++++++++---- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/proposals/readonly-locals-and-parameters.md b/proposals/readonly-locals-and-parameters.md index 26a2bea88f..3b61a4be68 100644 --- a/proposals/readonly-locals-and-parameters.md +++ b/proposals/readonly-locals-and-parameters.md @@ -9,8 +9,7 @@ In the name of Allah ## Summary [summary]: #summary - -The "readonly locals and parameters" feature is actually a group or features that declare local variables and method parameters without permit the state (objects or primitives) to modifications. +eThe "readonly locals and parameters" feature is actually a group or features that declare local variables and method parameters without permit the state (objects or primitives) to modifications. # Prevoius work @@ -29,9 +28,33 @@ constraint isn’t violated. In this scope C# has some features like readonly/const member, but has many missing features to help programmer to gain maximum immutability in program easily. One of these missing features has ability to declare local variables and method parameters without permit any modification after initialization. -## Solution - -## Syntax +## Detailed design +[design]: #detailed-design +We add new form to declaration statements: +``` antlr +declaration_statement: + local-variable-declaration + | local-constant-declaration + | local-readonly-declaration + ; +``` +### Local readonly declarations +A *local-readonly-declaration* declares one or more local readonly variables. +``` antlr +local-readonly-declaration: + readonly type readonly-declarators + ; + +readonly-declarators: + readonly-declarator + | readonly-declarators , readonly-declarator + ; + +readonly-declarator: + identifier = local-variable-initializer + ; +``` +Direct assignments to a local readonly variable are permitted only in `readonly-declarator` with `local-variable-initializer`. Attempting to assign to a local readonly variable is a compile-time error elsewhere. ## Drawbacks [drawbacks]: #drawbacks From 4af8a91cf132e2a03a1ad060fda807dc0aff8e8c Mon Sep 17 00:00:00 2001 From: soroshsabz Date: Tue, 28 Mar 2017 12:29:19 +0430 Subject: [PATCH 07/11] Update readonly-locals-and-parameters.md Added readonly parameter description. --- proposals/readonly-locals-and-parameters.md | 36 ++++++++++++++++++--- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/proposals/readonly-locals-and-parameters.md b/proposals/readonly-locals-and-parameters.md index 3b61a4be68..c1b4bf84bc 100644 --- a/proposals/readonly-locals-and-parameters.md +++ b/proposals/readonly-locals-and-parameters.md @@ -11,7 +11,7 @@ In the name of Allah [summary]: #summary eThe "readonly locals and parameters" feature is actually a group or features that declare local variables and method parameters without permit the state (objects or primitives) to modifications. -# Prevoius work +# Related work There is an existing proposal that touches this topics https://github.com/dotnet/roslyn/issues/115 and https://github.com/dotnet/csharplang/issues/188. Here I just want to acknowledge that the idea by itself is not new anyway. @@ -30,7 +30,10 @@ In this scope C# has some features like readonly/const member, but has many miss ## Detailed design [design]: #detailed-design -We add new form to declaration statements: + +### Readonly locals +We add new part to declaration statements: + ``` antlr declaration_statement: local-variable-declaration @@ -38,8 +41,10 @@ declaration_statement: | local-readonly-declaration ; ``` -### Local readonly declarations + +#### Local readonly declarations A *local-readonly-declaration* declares one or more local readonly variables. + ``` antlr local-readonly-declaration: readonly type readonly-declarators @@ -47,15 +52,36 @@ local-readonly-declaration: readonly-declarators: readonly-declarator - | readonly-declarators , readonly-declarator + | readonly-declarators ',' readonly-declarator ; readonly-declarator: - identifier = local-variable-initializer + identifier '=' local-variable-initializer ; ``` Direct assignments to a local readonly variable are permitted only in `readonly-declarator` with `local-variable-initializer`. Attempting to assign to a local readonly variable is a compile-time error elsewhere. +### Readonly parameters +We add new parts to parameter modifier: + +``` antlr +parameter_modifier: + 'ref' + | 'out' + | 'this' + | 'readonly' // new + | 'readonly ref' // new + | 'readonly this' // new + ; +``` +`readonly` modifier for parameter means that this parameter's state does not permit to change. So attempting to assign to a this parameter in method body is a compile-time error. + +`readonly` parameter can have *default-argument*. + +The `readonly`, `readonly ref` and `readonly this` are part of a method's signature. + +**Note:** `readonly ref` and `readonly this` are describe at [Readonly references proposal](readonly-ref.md). + ## Drawbacks [drawbacks]: #drawbacks From f91c4e95a3642974b32bdd88add8ea61fc44336c Mon Sep 17 00:00:00 2001 From: soroshsabz Date: Tue, 28 Mar 2017 12:32:57 +0430 Subject: [PATCH 08/11] Update readonly-locals-and-parameters.md Correct some mistake in grammar. --- proposals/readonly-locals-and-parameters.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/proposals/readonly-locals-and-parameters.md b/proposals/readonly-locals-and-parameters.md index c1b4bf84bc..91bec7c223 100644 --- a/proposals/readonly-locals-and-parameters.md +++ b/proposals/readonly-locals-and-parameters.md @@ -62,7 +62,7 @@ readonly-declarator: Direct assignments to a local readonly variable are permitted only in `readonly-declarator` with `local-variable-initializer`. Attempting to assign to a local readonly variable is a compile-time error elsewhere. ### Readonly parameters -We add new parts to parameter modifier: +We add new part to parameter modifier: ``` antlr parameter_modifier: @@ -70,15 +70,13 @@ parameter_modifier: | 'out' | 'this' | 'readonly' // new - | 'readonly ref' // new - | 'readonly this' // new ; ``` `readonly` modifier for parameter means that this parameter's state does not permit to change. So attempting to assign to a this parameter in method body is a compile-time error. `readonly` parameter can have *default-argument*. -The `readonly`, `readonly ref` and `readonly this` are part of a method's signature. +The `readonly` is part of a method's signature. **Note:** `readonly ref` and `readonly this` are describe at [Readonly references proposal](readonly-ref.md). From 4dc1315aec55051b23692287b8bbe0186c247459 Mon Sep 17 00:00:00 2001 From: soroshsabz Date: Thu, 30 Mar 2017 18:29:22 +0430 Subject: [PATCH 09/11] Update readonly-locals-and-parameters.md --- proposals/readonly-locals-and-parameters.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/proposals/readonly-locals-and-parameters.md b/proposals/readonly-locals-and-parameters.md index 91bec7c223..6c867859eb 100644 --- a/proposals/readonly-locals-and-parameters.md +++ b/proposals/readonly-locals-and-parameters.md @@ -1,5 +1,3 @@ -In the name of Allah - # Readonly locals and parameters * [x] Proposed From db4605d18753705d329970b09313c1c2b45d9286 Mon Sep 17 00:00:00 2001 From: soroshsabz Date: Thu, 30 Mar 2017 18:56:28 +0430 Subject: [PATCH 10/11] Update readonly-locals-and-parameters.md Some minor grammar improvements. --- proposals/readonly-locals-and-parameters.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/proposals/readonly-locals-and-parameters.md b/proposals/readonly-locals-and-parameters.md index 6c867859eb..b4ccba5be7 100644 --- a/proposals/readonly-locals-and-parameters.md +++ b/proposals/readonly-locals-and-parameters.md @@ -7,7 +7,8 @@ ## Summary [summary]: #summary -eThe "readonly locals and parameters" feature is actually a group or features that declare local variables and method parameters without permit the state (objects or primitives) to modifications. +The "readonly locals and parameters" feature is actually a group of features that declare local variables and method parameters without permission to modify the state (objects or primitives). +. # Related work @@ -16,7 +17,7 @@ Here I just want to acknowledge that the idea by itself is not new anyway. ## Motivation -Maximizing data and state immutability in any program has value that cause to improve readability, predictability and maintainability. So many languages have some concepts and features to provide some facilities about it and because above benefits these features are many uses in programs and master programmers recommended to use them, for example [Scott Meyers in Effective C++ book third edition](https://en.wikipedia.org/wiki/Scott_Meyers) says: +Maximizing data and state immutability in any program has value which causes improvements in readability, predictability, and maintainability. So many languages have some concepts and features to provide some facilities about it and because above benefits these features are many uses in programs and master programmers recommended to use them, for example [Scott Meyers in Effective C++ book third edition](https://en.wikipedia.org/wiki/Scott_Meyers) says: > Use const whenever possible And when he wants to describe it says: @@ -24,7 +25,7 @@ And when he wants to describe it says: >The wonderful thing about const is that it allows you to specify a semantic constraint — a particular object should not be modified — and compilers will enforce that constraint. It allows you to communicate to both compilers and other programmers that a value should remain invariant. Whenever that is true, you should be sure to say so, because that way you enlist your compilers’ aid in making sure the constraint isn’t violated. -In this scope C# has some features like readonly/const member, but has many missing features to help programmer to gain maximum immutability in program easily. One of these missing features has ability to declare local variables and method parameters without permit any modification after initialization. +In this scope C# has some features like readonly/const member, but there are also missing features for helping programmers to easily gain maximum immutability in programs. One of these missing features has ability to declare local variables and method parameters without permitting any modifications after initialization. ## Detailed design [design]: #detailed-design From e065014f02d58841e6bd4a6ebe69340c6220b01d Mon Sep 17 00:00:00 2001 From: soroshsabz Date: Thu, 30 Mar 2017 19:15:26 +0430 Subject: [PATCH 11/11] Update readonly-locals-and-parameters.md Some minor grammar improvements. --- proposals/readonly-locals-and-parameters.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/proposals/readonly-locals-and-parameters.md b/proposals/readonly-locals-and-parameters.md index b4ccba5be7..359bb841b5 100644 --- a/proposals/readonly-locals-and-parameters.md +++ b/proposals/readonly-locals-and-parameters.md @@ -17,7 +17,7 @@ Here I just want to acknowledge that the idea by itself is not new anyway. ## Motivation -Maximizing data and state immutability in any program has value which causes improvements in readability, predictability, and maintainability. So many languages have some concepts and features to provide some facilities about it and because above benefits these features are many uses in programs and master programmers recommended to use them, for example [Scott Meyers in Effective C++ book third edition](https://en.wikipedia.org/wiki/Scott_Meyers) says: +Maximizing data and state immutability in any program has value which causes improvements in readability, predictability, and maintainability. So many languages have some concepts and features which provide facilities about them, and because of mentioned benefits, these features have many uses in programs, and master programmers have recommended using them, for example [Scott Meyers in Effective C++ book third edition](https://en.wikipedia.org/wiki/Scott_Meyers) says: > Use const whenever possible And when he wants to describe it says: