-
Notifications
You must be signed in to change notification settings - Fork 21
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
Comments
I think it must be type agnostic for any type supporting 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 |
Wouldn't @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
] |
Then maybe let map = [
["key1"] <- "value1"
["key1"] <- "value1"
] or let map = [
.["key1"] <- "value1"
.["key1"] <- "value1"
] |
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 |
I'm going to close this. It's little known but you can already do
You can also do
So if anything we should consider deprecating |
Is it a real collection initializer and not creating list and then a set or map? |
As things stand, it creates the intermediate list |
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. |
Change from |
Just for reference, it is possible to make a As to whether such a thing should be in the core library: that depends on whether type-direction for |
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. |
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
The text was updated successfully, but these errors were encountered: