I suggest we ...

Length constraints on lists and arrays

Whilst you can pattern match on lists in order to get compiler support on the length of them, it would be great if we had compiler support for list length for e.g. arguments to functions. Things like Seq.head or even list destructuring can't be proven to not go pop except through pattern matching, but if we had the ability to add length constraints to e.g. a list passed in as an argument to a function this would simplify lots of boilerplate code e.g.

let foo(myListOfItems:int list<3>) = () // myListOfItems must be three elements long.

or even

let foo ([ a;b;c;]) = () // automatically unwrap the list of three elements into a b and c.

Not saying that the above syntax would necessarily be the way to go - just ideas as to achieving the aim of getting better compiler support for working with lists.

3 votes
Vote
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    Signed in as (Sign out)
    You have left! (?) (thinking…)
    Isaac Abraham shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

    6 comments

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      Signed in as (Sign out)
      Submitting...
      • Don Syme commented  ·   ·  Flag as inappropriate

        It is possible to deal with some special cases via integer parameters or other existing uses of constraint solving. However that can lead the programmer to use the feature during problem exploration, but then the feature falls down at some point. The programmer must then back out and remove the use of the feature.

      • Richard Gibson commented  ·   ·  Flag as inappropriate

        You could write a function now to currently turn a tuple of identical items into a sequence, such as:

        let asSeq (one, two, three) = seq { yield one; yield two; yield three }

        Perhaps you could use a type provider to generate useful functions or extension methods for you.

        As for language support, perhaps it would be better to try and get special support from the compiler to let you treat Tuple<T, T, T, ...> as seq<T>.

      • Isaac Abraham commented  ·   ·  Flag as inappropriate

        @Vasily - I know diddly about writing compilers :-) But can this not be done with things like units of measure i.e. metadata that gets erased at runtime; could this not be done in a similar manner?

        @Daniel - not really - you can't use all the list or sequence operations etc. on them.

      • Vasily Kirichenko commented  ·   ·  Flag as inappropriate

        As far as I understand this requires dependent types which are very, very unlikely to be added to F# in any observable future.

      F# Language

      Feedback and Knowledge Base