Pre-SIP: a syntax for aggregate literals

This would make a lot of sense, but IMO this is a place where convenience may trump simplicity. Just as Seqs are a pretty fundamental data type, so are Maps, so having a special syntax for both of them is not unreasonable.

The basic idea that this proposal comes from is that there are some fundamental data structures that are broadly useful across all programming languages and environments, and deserve a literal syntax. Scala already inherited literal numbers, literal strings, literal booleans from Java, and this proposal would add literal sequences and (maybe) literal maps. It’s always debate-able where to draw the line, but IMO the general popularity of key-value maps in all configuration formats and all scripting languages does indicate that they are broadly useful enough to deserve a bit of special casing

Just to chip in from a library author perspective, having the aggregate literal syntax work for nested literals is table stakes for most of my use cases. e.g. You can currently write

val json0 = ujson.Arr(
  ujson.Obj("myFieldA" -> ujson.Num(1), "myFieldB" -> ujson.Str("g")),
  ujson.Obj("myFieldA" -> ujson.Num(2), "myFieldB" -> ujson.Str("k"))
)

val json = ujson.Arr( // The `ujson.Num` and `ujson.Str` calls are optional
  ujson.Obj("myFieldA" -> 1, "myFieldB" -> "g"),
  ujson.Obj("myFieldA" -> 2, "myFieldB" -> "k")
)

And part of the draw for this syntax is to be able to concisely write:

val json0: ujson.Value = [
  ["myFieldA" -> 1, "myFieldB" -> "g"],
  ["myFieldA" -> 2, "myFieldB" -> "k"]
]

This also applies to heterogenous use cases, so that rather than writing

val expected = ujson.Obj(
  "hello" -> ujson.Arr("hello", "world"),
  "hello2" -> ujson.Obj("1" -> "hello", "2" -> "world")
)

I would want to be able to write

val expected: ujson.Value = [
  "hello" -> ["hello", "world"],
  "hello2" -> ["1" -> "hello", "2" -> "world"]
]
6 Likes