-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move mutating functions in the Array
module to a new Array.InPlace
/ Array.Mutating
#1080
Comments
The biggest disadvantage is that this is a sweeping breaking change :) |
Also this is in line with the Theme of "Simple F#": https://github.com/dotnet/fsharp/issues?q=label%3ATheme-Simple-F%23+ |
It's as breaking as deprecating |
I'm not aware of |
The title did not use "deprecate" then "add" since it will be too long. The first sentence in the description is:
|
At a bare minimum, wouldn't it make more sense to allow optional opening of a module that shadows standard core behavior with mutable defaults? |
To be clear: the renaming also concerns me, since that breaks swaths of code, and love it or not, we have For cases like |
Not possible because array resizing / type-changing in-place and returning an array type requires additional allocations. We can make use of ArraySegment / Span / ReadOnlySpan, but piping is gone and additional modules are required. |
Okay, well we can argue this to death and ensure nothing happens here, but as it stands I don't support this. |
The function signatures already serve to distinguish mutating functions from non-mutating. With the proposed changes we trade a function signature that clarifies that a function is mutating for the assumption that all functions within a certain module are mutating. Personally I would prefer to rely on the type signatures than an assumption that all functions in a module conform to an idea the module is named after. |
And they are confusing to new users because they sit alongside copying functions that are to be expected in other collection modules. |
You already lose |
What if instead there was an attribute which allows you to easily see which functions in the standard lib mutate or may mutate? It often makes sense to both return something AND mutate, but it would be nice if we had a signal to the consumer of that function that we could use to let them know "Hey this mutates". A function that isn't tagged may or may not mutate, but it would be nice to explicitly know that some do mutate. Perhaps you could even scan to look for functions which call functions/methods that return unit and generate the annotation. I do agree with @cartermp that this as proposed a big breaking change that will upset people regardless of how good it is, because some people have massive codebases and they'll reasonably interpret "deprecated" as "change it". Being said your Array.InPlace might be a good nuget package to make. |
When I was starting with F# I was introduced to Array and List as immutable collections. When I first saw a method that mutated the array I was totally confused. So is it immutable or not? How can I now trust that it's immutable when I can easily mutate it. I like moving these to a separate module because it clearly shows that you're mutating the array at first glance and that those should only be used if you know what you're doing. Also personally I don't really have a problem with the deprecation. Especially if we can have a quick-action and one-click apply it in the whole solution to change everything. |
I'd be curious where - since the langref is clear about cell-mutability. This is behavior going back 4 decades or more in the ML family of languages - so any source citing that arrays are immutable is suspect. |
So the story could be very similar to this article: https://www.compositional-it.com/news-blog/immutable-data-structures-in-f/ People would talk about why F# is cool and why immutable collections are the way to go and then they use Array. Maybe not explicitly stating that array is immutable but I hope you can see how somebody who didn't read the manual might assume. There's also the part where most of the time you'd use methods that don't mutate the array so you're not exposed to the mutability aspect of Array much. Anyway, Just trying to highlight the positives of this change. In my humble opinion, it does make the behavior of the functions a bit more explicit and organized which might help some people. |
The Array module should stay immutable, while the mutability of arrays themselves are too prevalent to change. We should stay to the module functions to guarantee immutability. |
How about |
I agree with comments above that it's too much of a breaking change all in all As a result I will close this |
How come |
I understood from this that the agreed solution to deprecating mutating array functions was to introduce a new immutable array More on block here: #619 |
like any mutations can still go through the indexer as well as the new Array.InPlace module. Mutating is still possible but we can trust Array module functions to be immutable. |
Because throwing an exception and mutating aren't the same thing? 😐 |
Move mutating functions in the
Array
module to a newArray.InPlace
/Array.Mutating
I propose we deprecate:
and add (using
InPlace
as an example)We can also consider adding
and deprecating
in the process.
The existing way of approaching this problem in F# is manually checking which function mutates among lots of copying functions in the same module.
Pros and Cons
The advantages of making this adjustment to F# are
.InPlace
/.Mutating
The disadvantages of making this adjustment to F# are
unit
. This makes theunit
-returning = mutating harder to follow. However, existing C# fluent dotted calls already do this.Array.Parallel
, these are only here for optimisation and you should always default to the normalArray
functions. WithArray.Parallel
's precedence, the risk of encouraging mutable programming is minimised.Extra information
Estimated cost (XS, S, M, L, XL, XXL): M to L
Related suggestions: #508
Affidavit (please submit!)
Please tick this by placing a cross in the box:
Please tick all that apply:
For Readers
If you would like to see this issue implemented, please click the 👍 emoji on this issue. These counts are used to generally order the suggestions by engagement.
The text was updated successfully, but these errors were encountered: