I suggest we ...

Add a shorthand/literal syntax for creating Maps

A shorthand/literal syntax or computation expression for building Maps would be nice—something like the following:

let m = map ["a",97; "b",98; "c",99]

The shorthand for dictionaries ...

let d = dict ["a",97; "b",98; "c",99]

... is nice, but while Maps implement IDictionary, dictionaries do not play well with Map module functions. One must inevitably convert a dictionary to a Map or create a Map with Map.ofArray, .ofList, or .ofSeq.

3 votes
Vote
Sign in
(thinking…)
Sign in with: facebook google
Signed in as (Sign out)
You have left! (?) (thinking…)
Brad Collins shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

4 comments

Sign in
(thinking…)
Sign in with: facebook google
Signed in as (Sign out)
Submitting...
  • Brad Collins commented  ·   ·  Flag as inappropriate

    @ak, ah, I see! I am embarrassed that I had not explored that option. But I see now that it's merely a call to the Map constructor.

    I will quibble that the style is inconsistent. That is, it's set [ ... ] for sets, dict [ ... ] for dictionaries, seq [ ... ] for sequences, but it's Map [ ... ] for maps. That inconsistency is not satisfying.

    Nevertheless, I concede the point that a shorthand clearly already exists. I just overlooked it.

  • AK commented  ·   ·  Flag as inappropriate

    You can already do this

    i.e Map [("Key", "Value); ("Key2", "Value2")] works fine in my code. Notice the capital M on Map

  • Brad Collins commented  ·   ·  Flag as inappropriate

    @amazingant, no argument that it's an easy fix, but its seems like an obvious oversight (and perhaps even that's too strong a word) when we have shorthand for every other built-in F# container: lists, sequences, arrays, and sets. As someone who came to the language just a little over a year ago, I found it incongruous for map shorthand to be missing.

  • amazingant commented  ·   ·  Flag as inappropriate

    You can add this yourself fairly easily by adding the following anywhere in your code:

    let map = Map.ofList

    and then your first code sample works

F# Language

Feedback and Knowledge Base