-
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
for…in…while #1400
Comments
While I understand that you want to preserve somewhat "functional" approach I think this will be more limiting vs simply introducing "break" and "return" into the language. I do miss them myself quite often. I'm not a fan of using recursion where it is not a recursive thing in nature but a workaround. And when used in such way it often makes code worse both performance and readability wise. |
I think the benefit over using |
This makes me miss (iter (for pattern in enumerable-expression)
(while test-expression)
body-expression) That being said, I agree with @En3Tho: this is nice but ultimately a less powerful construct than |
I understand the concerns. If Without a way to cleanly exit a That being said, if |
For the specific scenario of byref-like types, I think the "allows ref struct" feature, if we ever support that, would mean that you could write the equivalent of For non-byref-like types, you could use a computation expression if you wanted... Example 1let chars = "asdf lol"
whilst (not << Char.IsWhiteSpace) { for c in chars do printfn $"{c}" } Example 2let chars = "asdf lol"
breakable { for c in chars, not << Char.IsWhiteSpace do printfn $"{c}" } (You could probably write one that used a custom operation to specify the predicate as well.) |
I propose we have a new construct combining
while
andfor…in
that iterates over a collection while a condition is maintained, i.e.for…in…while
:This construct shares the behaviour of both
for…in
andwhile
, i.e. it evaluatestest-expression
upon each entry intobody-expression
, and oncetest-expression
isfalse
, it stops the iteration.test-expression
may use any variables declared bypattern
.The existing way of approaching this problem in F# is to use
Seq.takeWhile
or to lower the code manually to useGetEnumerator
andwhile
.Pros and Cons
The advantages of making this adjustment to F# are simplicity of use of
for
with breaking conditions in situations where a more traditional rewriting into purely functional code is unwieldy or impossible, for example when byref-like types are involved, as well as other enumerable types in .NET that do not implementIEnumerable
. It is also better to have a dedicated construct for this than to useSeq.takeWhile
in case the condition is not pure (e.g. it depends onbody-expression
). In the case of manual lowering usingGetEnumerator
, it is prone to mistakes (for example if one forgets to callDispose
), and misses out on optimizations that F# performs forfor
for ranges, lists, arrays, or spans.The disadvantages of making this adjustment to F# are having yet another "imperative" construct that people may pick instead of expressing the intention in a more idiomatic way. However, sometimes there are no better options (e.g. spans) and the same can be argued against
while
too.Extra information
Estimated cost (XS, S, M, L, XL, XXL): M
Related suggestions: #381 (another approach using
break
)Affidavit
This is not a question (e.g. like one you might ask on StackOverflow) and I have searched StackOverflow for discussions of this issue
This is a language change and not purely a tooling change (e.g. compiler bug, editor support, warning/error messages, new warning, non-breaking optimisation) belonging to the compiler and tooling repository
This is not something which has obviously "already been decided" in previous versions of F#. If you're questioning a fundamental design decision that has obviously already been taken (e.g. "Make F# untyped") then please don't submit it
I have searched both open and closed suggestions on this site and believe this is not a duplicate
This is not a breaking change to the F# language design
I or my company would be willing to help implement and/or test this
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: