I suggest we ...

Add ofObj to Seq and Array

The Option module defines Option.ofObj which converts a potential nullable value to an option.

Collections (Seq and array) can be null in interop scenarios, but it'd often be natural to interpret a null collection as an empty collection.

It's possible to compose such behaviour from existing building blocks, e.g. with Option.ofObj >> Option.toArray >> Array.concat

This seems like quite a roundabout way to do things, so I'd like to propose equivalent functions for Seq and Array:

// seq<'a> -> seq<'a>
Seq.ofObj

// 'a [] -> 'a []
Array.ofObj

6 votes
Vote
Sign in
(thinking…)
Sign in with: Facebook Google
Signed in as (Sign out)
You have left! (?) (thinking…)
Mark Seemann shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

1 comment

Sign in
(thinking…)
Sign in with: Facebook Google
Signed in as (Sign out)
Submitting...
  • Loic Denuziere commented  ·   ·  Flag as inappropriate

    Alternatively for something more general, we could have a null equivalent of `defaultArg`. So if it's called let's say `ifNull` (placeholder name, not actual proposal) then your `Seq.ofObj s` would be `ifNull s Seq.empty`, and `Array.ofObj s` would be `ifNull s [||]`.

F# Language

Feedback and Knowledge Base