C# allows the following object initialization syntax for classes with Item indexer:
using System.Text.Json.Nodes;
namespace JsonNodeFromObjectExample;
...
// Create a new JsonObject using object initializers.
var forecastObject = new JsonObject
{
["Date"] = new DateTime(2019, 8, 1),
["Temperature"] = 25,
["Summary"] = "Hot",
["DatesAvailable"] = new JsonArray(new DateTime(2019, 8, 1),
new DateTime(2019, 8, 2)),
["TemperatureRanges"] = new JsonObject
{
["Cold"] = new JsonObject
{
["High"] = 20,
["Low"] = -10
}
},
["SummaryWords"] = new JsonArray("Cool", "Windy", "Humid")
};
...
In F# I can
let forecastObject = JsonObject()
forecastObject["Date"] <- DateTime(2019, 8, 1)
forecastObject["Temperature"] <- 25
...
But I need to repeatedly write the object name, which is uglier, less succinct and more error prone.
A potentially equivalent syntax would be the one available for properties initialization:
let forecastObject = JsonObject(
["Date"] = DateTime(2019, 8, 1),
["Temperature"] = 25,
...
)
However it doesn't work with indexers like Item().
Does anyone know if there is an equivalent syntax in F# like there is in C#?