misko
02/06/2018, 10:20 AMclass Json {
val map : HashMap<String, Any> = HashMap()
}
inline operator fun <V> Json.set(key: String, value: V) {
this.map[key] = value as Any // this is not of importance
}
fun obj(builder: Json.()->Unit): Json {
val obj = Json()
builder.invoke(obj)
return obj
}
fun main(args: Array<String>) {
obj {
set("1", 1) // this is ok
this["2"] = 2 // this is ok
["3"] = 3 // this is not - Kotlin: Unsupported [Collection literals outside of annotation]
// - Kotlin: Variable expected
}
}
voddan
02/06/2018, 10:30 AMbenleggiero
02/07/2018, 2:47 AM["3"]
looks like a collection literal to me. Kotlin doesn’t have these, but your proposed syntax would eliminate the future possibility