47degrees / helios

A purely functional JSON library for Kotlin built on Λrrow
https://47degrees.github.io/helios/
Apache License 2.0
169 stars 22 forks source link

Weird 'add' function #118

Open ambrusadrianzh opened 5 years ago

ambrusadrianzh commented 5 years ago

Currently the add function looks like this: fun add(key: String, value: Json): JsObject = JsObject(hashMapOf(key to value))

It's a bit confusing. Is it intentional? Maybe as an of it would be clearer.

nomisRev commented 5 years ago

I agree, it's not intentional but we've not added any sugar constructors or not many.

We should add:

data class JsObject(...): Json() {
   companion object {
      operator fun invoke(vararg: Pair<String, Json>): JsObject = ...
   }
}

Which would allow following syntax: JsObject(key to value)

lostintime commented 4 years ago

IMO expected behavior for add method would be to add new object field to called instance:

fun add(key: String, value: Json): JsObject = JsObject(this.value + hashMapOf(key to value))
nomisRev commented 4 years ago

Yes, that's definitely the expected behavior of add.