https://kotlinlang.org logo
#announcements
Title
# announcements
s

Shawn

06/16/2019, 4:12 PM
Copy code
val http200Status = object { val statusCode = 200; val description = "OK" }

println("The status code is ${http200Status.statusCode}")
👆 1
👍 1
w

wcaokaze

06/16/2019, 4:15 PM
yes, but only available for local variables or private properties, right?
s

Shawn

06/16/2019, 4:15 PM
Indeed
I’m not sure if swift tuples have access to the named properties outside of those scopes, though - haven’t used them myself
w

wcaokaze

06/16/2019, 4:17 PM
Swift can do it 😣
m

Mike

06/16/2019, 4:33 PM
If you want access like that, you can use a map. Otherwise, I tend to favour concrete classes anyway. The whole point of using a static language, right?
s

Shawn

06/16/2019, 4:34 PM
I think the point of the Swift implementation is that (somehow) you get the best of both worlds
I wouldn’t say that that’s the whole point of using a static type system ¯\_(ツ)_/¯
m

Mike

06/16/2019, 4:35 PM
Although thinking about it, it's not really a map, is it as I would guess the names are known to the compiler. So it's sort of an easy data class. I'd have to see more examples of usage as the one provided doesn't feel like a good use case. I'd define an httpstatus class with those properties and use it for 200, 404 etc.
j

Jan Stoltman

06/16/2019, 9:11 PM
I don't think there is any real-life use case where such tuples could be somehow superior to small-ish data classes. But it seems like a nice feature for readability, instead of the old first/second/destructuring
g

gildor

06/17/2019, 2:33 AM
Why not just use data class for this?
Copy code
data class HttpStatus(val statusCode: Int, description: String)

val http200Status = HttpStatus(200, "OK")

println("The status code is ${http200Status.statusCode}")
// Prints "The status code is 200"
println("The status message is ${http200Status.description}")
j

Jan Stoltman

06/17/2019, 12:29 PM
Convenience for small-scope pair operations? Wherever data class is kind of an overkill but names could still be useful
g

gildor

06/17/2019, 12:37 PM
For small local scope you can use
object { }
👍 3
2 Views