I suggest we ...

Code clarity: Make syntax for sequence expressions as simple as syntax for lists

I suggest simplifying syntax for generating sequences. It would be great to make it as simple as syntax for lists in F#.

For example,

let lst = [ 1; 2; 3; 4 ] // allowed
let sq = seq { 1; 2; 3; 4 } // not allowed, why?
// error FS0739: Invalid object, sequence or record expression.

Now, the F# syntax rules allow declaring a simple sequence expression as follows:

let se = seq { yield 1; yield 2; yield 3 }

I think syntax rules may well be simplified in that case. It would be more comfortable for language users to declare sequences with no need to write the yield keyword each time when they specify a new value for the sequence expression.

P.S. At the same time, there is no need to make any improvements in syntax for generating sequences using ranges specified in the form like 1 .. 10.
let lst = [ 1 .. 10 ] // allowed
let sq = seq { 1 .. 10 } // allowed, and that's good!

4 votes
Sign in
Sign in with: facebook google
Signed in as (Sign out)
You have left! (?) (thinking…)
Alexei Odeychuk shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →


Sign in
Sign in with: facebook google
Signed in as (Sign out)
  • Jared Hester commented  ·   ·  Flag as inappropriate

    However this would not be consistent with the rules for computation expressions.

    "Sequence expressions are an example of a computation expression, as are asynchronous workflows and query expressions." [1]

    If you're using a seq expr it should be able to take advantage of complex internal logic, recursive looping, lifting nested sequences with yield!, etc.
    Vasily is right, if all you want is a simple sequence use

    seq [10;55;0] or seq [| 40; 5; 0; 40 |]|

    Perhaps that error should be improved to say "invalid sequence expression ..." with some more details and a suggestion

    [1] https://msdn.microsoft.com/en-us/visualfsharpdocs/conceptual/computation-expressions-%5Bfsharp%5D

  • Alexei Odeychuk commented  ·   ·  Flag as inappropriate

    Thank you, Vasily. Of course, I can, but let sq = seq { 1; 2; 3 } would be better and consistent with the existing F# syntax rules for sequence expressions, I mean the use of {}.

F# Language

Feedback and Knowledge Base