Hullaballoonatic
09/13/2018, 6:03 PMwith and how do I make use of it?Shawn
09/13/2018, 6:05 PMwith is a function that takes an object and a lambda that lets you use the object you pass in as if it were thisHullaballoonatic
09/13/2018, 6:05 PMval person = with(Person()) {
name = "Tony Stark"
age = 52
// More such stuff
this
}Shawn
09/13/2018, 6:05 PMCalls the specified functionwith the given[block]as its receiver and returns its result.[receiver]
Hullaballoonatic
09/13/2018, 6:06 PMperson just an instance of Person?Shawn
09/13/2018, 6:06 PMthis at the end of the lambda, maybe just use .apply { ... } instead?Hullaballoonatic
09/13/2018, 6:06 PMsupotuco
09/13/2018, 6:07 PMwith helps make code look nicerHullaballoonatic
09/13/2018, 6:07 PMShawn
09/13/2018, 6:08 PMHullaballoonatic
09/13/2018, 6:08 PMHullaballoonatic
09/13/2018, 6:09 PMShawn
09/13/2018, 6:09 PMval person = Person().apply {
name = "Tony Stark"
age = 52
}
would be more idiomatic but ¯\_(ツ)_/¯Hullaballoonatic
09/13/2018, 6:09 PMShawn
09/13/2018, 6:09 PMShawn
09/13/2018, 6:10 PMHullaballoonatic
09/13/2018, 6:11 PMHullaballoonatic
09/13/2018, 6:13 PMHullaballoonatic
09/13/2018, 6:13 PMsupotuco
09/13/2018, 6:14 PMHullaballoonatic
09/13/2018, 6:16 PMforEach and a few other things like elvis operators, but I never have used stuff like let, with, apply, destructuring declarations, etc, and my code would be a lot better if i did. i imagine...Hullaballoonatic
09/13/2018, 6:39 PMhudsonb
09/13/2018, 6:56 PMPerson(init: () -> Person) constructor or top-level method so you can do:
val person = Person {
name = "Tony Stark"
age = 32
}Hullaballoonatic
09/13/2018, 6:56 PMHullaballoonatic
09/13/2018, 6:56 PMShawn
09/13/2018, 6:58 PMhudsonb
09/13/2018, 6:59 PMHullaballoonatic
09/13/2018, 6:59 PMhudsonb
09/13/2018, 7:00 PMfun person(init: Person.() -> Unit) = Person().apply(init)Hullaballoonatic
09/13/2018, 7:03 PMHullaballoonatic
09/13/2018, 7:04 PMHullaballoonatic
09/13/2018, 7:04 PMilya.gorbunov
09/13/2018, 7:41 PMbkenn
09/13/2018, 7:45 PMdata class Person(var name: String = "", var age: Int = -1)
fun Person(op: Person.() -> Unit) = Person().apply(op)
val brian = Person {
name = "Brian"
age = 26
}
fun main(args: Array<String>) {
println(brian)
}bkenn
09/13/2018, 7:45 PMbkenn
09/13/2018, 7:46 PMHullaballoonatic
09/13/2018, 7:46 PM