Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a shorthand/literal syntax for creating Maps #49

Closed
baronfel opened this issue Oct 20, 2016 · 11 comments
Closed

Add a shorthand/literal syntax for creating Maps #49

baronfel opened this issue Oct 20, 2016 · 11 comments

Comments

@baronfel
Copy link
Contributor

Add a shorthand/literal syntax for creating Maps [12536532]

Submitted by Brad Collins on 3/1/2016 12:00:00 AM
3 votes on UserVoice prior to migration

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.

Original UserVoice Submission
Archived Uservoice Comments

@xperiandri
Copy link

I think it must be type agnostic for any type supporting KeyValuePair
Something similar to C# like

let map = [
    ["key1"] = "value1"
    ["key1"] = "value1"
]

let map : ImmutableDictionary<string, string> = [
    ["key1"] = "value1"
    ["key1"] = "value1"
]

So that if type supports constructor or AddRange(KeyValuePair<'TKey,'TValue> seq) it will work

@jwosty
Copy link
Contributor

jwosty commented Aug 31, 2021

Wouldn't let map = Map.ofSeq work for the usage that OP provided?

@xperiandri Good idea -- if we're going to make this a language suggestion, it would need to be a meaningful extension to the language like you're suggesting. However, your proposed syntax would be ambiguous (could be a bool list). Perhaps something more like the following (though I don't like the usage of naked square brackets, as there's still some ambiguity there between the list syntax):

let map = [
    "key1" -> "value1"
    "key2" -> "value2"
]

Side note: you hack something together (for maps in particular) that syntactically gets you pretty close in today's F# (weird, but fun):

let map = Map.ofSeq
let (=>) key value = key, value // since you can't have a -> operator
let xs = map [
    "key1" => 42
    "key2" => 43
]

@xperiandri
Copy link

Then maybe

let map = [
    ["key1"] <- "value1"
    ["key1"] <- "value1"
]

or

let map = [
    .["key1"] <- "value1"
    .["key1"] <- "value1"
]

@xperiandri
Copy link

xperiandri commented Aug 31, 2021

if we're going to make this a language suggestion

There is a similar discussion about collections #619

I suppose these can be combined into a singles suggestion that will allow initializing collections and maps using let collection : CollectionType = [] syntax

@dsyme
Copy link
Collaborator

dsyme commented Oct 13, 2021

I'm going to close this. It's little known but you can already do

Map [ (1,3); (3,4) ]

You can also do

Set [ 1;3;4 ]

So if anything we should consider deprecating set [1;3;4] to have uniformity

@dsyme dsyme closed this as completed Oct 13, 2021
@xperiandri
Copy link

Is it a real collection initializer and not creating list and then a set or map?

@dsyme
Copy link
Collaborator

dsyme commented Oct 13, 2021

As things stand, it creates the intermediate list

@Lanayx
Copy link

Lanayx commented Jan 27, 2024

This issue might we relevant to type-directed collection initialization #1086. I'd like to see the type-directed resolutions for Map, Dictionary, Sets, IDictionary and classes that inherit from Dictionary and IDictionary as well.

@abelbraaksma
Copy link
Member

Change from completed -> not planned

@abelbraaksma abelbraaksma closed this as not planned Won't fix, can't repro, duplicate, stale May 16, 2024
@brianrourkeboll
Copy link

Just for reference, it is possible to make a Map<_, _> builder that works with no intermediate collections: https://github.com/brianrourkeboll/FSharp.Collections.Builders.

As to whether such a thing should be in the core library: that depends on whether type-direction for […] were ever going to support maps or dictionaries as a target. C# does seem on track to add dictionary initializer syntax, for what it's worth.

@abelbraaksma
Copy link
Member

Yes, good points. Perhaps we should create a separate issue to collect what we currently know about type-directed collection initializers. I believe there are some other issues lying around that aren't specific to Maps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants