-
Notifications
You must be signed in to change notification settings - Fork 790
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
WIP: Syntax for creating ImmutableArrays #12859
base: main
Are you sure you want to change the base?
Conversation
tests/FSharp.Compiler.Service.Tests/FSharp.CompilerService.SurfaceArea.netstandard.actual was added by a test: should this be gitignored? |
Main thing needed I think it to implement |
It can be ignored, I guess, its purpose is to generate new surface area when public APIs have changed. Normally, you just move it to the normal surface area file replacing it. |
Test failures:
|
I'd like to compile and run this to see if this PR works: let x = [: "Hello world" :]
Console.WriteLine(x.Slice(0,0).[0].ToString()) Or alternatively write a test to this effect. Is there a best way to do this? An existing place where there are tests which compile and run something? |
When I want to test changes, I often just do |
Thanks to those helping out in the amplifying F# session! |
Hi @charlesroddie, I just took another look at this and I think we can try and prevent the transformation from If fsharp/src/Compiler/Checking/CheckComputationExpressions.fs Lines 2269 to 2294 in afabd07
Something like: match cType, comp with
| (CollectionType.Array | CollectionType.List), SimpleSemicolonSequence cenv acceptDeprecatedIfThenExpression elems -> If the That only leaves us with the empty immutable array case, |
That is actually only true for expressions. Patterns and nice print should still be addressed as well. |
@charlesroddie I think we also need to put this behind a LangPreview flag |
Yeah, that definitely needs to go under flag, but it should be relatively easy addition, we can first wrap the codegen and solve open questions. Not still entirely sure about recursive tast approach VS just emitting into codegen directly. Cc @dsyme |
It would be ideal to emit codegen to convert |
This should be the case for const arrays, yes. For comprehensions, we should follow what we do for lists. |
Would be good to know if there is an interest on completing/including this in compiler and library. If not what is the alternative? I know Avalonia.FuncUI and Fabulous will benefit a lot from this. |
Probably not in this form, but rather type-directed/user-defined collections as a more generic approach. |
I'm in favour of a generic approach 👍 Think C# 12's collection expressions are quite handy in that regard. |
Would the team have availability to tackle this one or is up external contributors to work on this ? |
Not for F# 9 for sure for the team. It will need to go through suggestion (might be there already), design, RFC, review. |
Maybe this fsharp/fslang-suggestions#1086 ? |
Not only that one, user-defined collections should also be considered (I think they're in the immutable arrays discussion somewhere in comments). |
In other words - suggestion would be something along the lines of "Allow collections to be type-directed, allow use of [...] syntax for them, and (optionally) allow user defined collections to follow the same rules.". First two sound straightforward, whereas last one is a bit more controversial (as well as not fully defined, I think). |
This is the one for the last point: fsharp/fslang-suggestions#625 As much as I hate "duplicate" suggestions, I think a new one should be made superseding and if accepted in more or less generic form, it will cover #15437 as well. Personally, my problem is that if we introduce another non-directed syntax for immutable arrays, it might be confusing people (i.e. having all of the I do also see issues with type direction which folks were concerned about in the corresponding suggestion, but I trust we can find middle ground for that. |
Thanks @vzarytovskii |
Getting this PR ready shouldn't take long. I can fix conflicts. It currently works for
Type-directed let l: ImmutableArray<int * string> = [ 0, "0"; 1, "1" ] instead of let l = [: (0, "0"); (1, "1") :] The former syntax then no one would use since it is far too verbose, and inferior to what you can use at the moment
Perhaps you could ask people if they would be confused by that? Is anyone confused by |
The official status of language discussion is: Both were proposed tentatively by @dsyme, and neither suggestion is a rejection of explicit syntax. For example we could introduce |
That is not what type-direction means in this particular case. It's all about: let someFunction (x: ImmutableArray<int>) = ...
// Right now, you should explicitly call it/construct it, whereas with working type-direction, the following will be possible
someFunction [1; 2; 3] It's not a repolacement for existing syntax for arrays, lists, how you do sets, etc, it's just to provide ability to use unified syntax where type is known. Also, with the type-direction (putting user-defined let inline imarray (xs: ImmutableArray<'T>) = xs
let inline array (xs: Array<'T>) = xs
let inline list (xs: List<'T>) = xs
let inline queue (xs: Queue<'T>) = XS
// and have the ability to declare collections as
let ary = immaray [1;2;3;4]
let l = list [1;2;3;4]
Yeah, every single time I show F# to new people/people from different languages, the question is "why can't compiler decide what's better for me by default? I just want to use []` All I'm saying is that I think, that this problem should be tackled at the more fundamental level, and not just "let's slap new syntax here, and all is good". Again, not saying we don't want to have "new syntax" for immutable array. It's maybe just better to have it as a compiler feature, and not compiler intrinsic. That's why I suggest we revisit the above suggestions, discussions to see what's is the thing we really want. |
But neither has consensus either unfortunately. |
Allow creating an ImmutableArray using
[: :]
.https://github.com/fsharp/fslang-design/blob/main/RFCs/FS-1094-block.md
Module functions done in #11750