C# Collection Initializer

F# can almost do the same with C# Object Initializer, but how about Collection Initializer? I’m using fold to call .Add() today but wonder if there’s any better way.

(splitting my post into two parts to circumvent Discourse’s URL count limit)

Use case: Class InputMap<V> from Pulumi

Well, for this particular case, I think the API should be improved (e.g. by taking IDictionary<string, V>) but that’s probably another story.

That isn’t possible in F#. I would just suggest that using a plain old for loop would be better than a fold. It’s simpler and more obvious that you’re performing a side-effect

1 Like