https://kotlinlang.org logo
Title
h

Hullaballoonatic

09/13/2018, 6:03 PM
what is
with
and how do I make use of it?
s

Shawn

09/13/2018, 6:05 PM
with
is a function that takes an object and a lambda that lets you use the object you pass in as if it were
this
h

Hullaballoonatic

09/13/2018, 6:05 PM
val person = with(Person()) {
      name = "Tony Stark"
      age = 52
      // More such stuff
      this
}
s

Shawn

09/13/2018, 6:05 PM
from the docs
Calls the specified function
[block]
with the given
[receiver]
as its receiver and returns its result.
h

Hullaballoonatic

09/13/2018, 6:06 PM
what is the difference between this and making
person
just an instance of
Person
?
s

Shawn

09/13/2018, 6:06 PM
if you want to return
this
at the end of the lambda, maybe just use
.apply { ... }
instead?
h

Hullaballoonatic

09/13/2018, 6:06 PM
yeah i read the docs, it was just hard to wrap my head around a case in which that is useful
s

supotuco

09/13/2018, 6:07 PM
with
helps make code look nicer
h

Hullaballoonatic

09/13/2018, 6:07 PM
i grabbed the above code from an example i looked up
s

Shawn

09/13/2018, 6:08 PM
the use in your specific example would be to have a more verbose initialization process, with named parameters and maybe some other method calls before passing on the object you initialized as a return value or as just a variable
h

Hullaballoonatic

09/13/2018, 6:08 PM
hmmm
i wanna be able to use all these kotlin features to make my code readable, and that sounds like it could help a lot
s

Shawn

09/13/2018, 6:09 PM
I still think
val person = Person().apply {
  name = "Tony Stark"
  age = 52
}
would be more idiomatic but ¯\_(ツ)_/¯
👍 2
h

Hullaballoonatic

09/13/2018, 6:09 PM
like could use that to implement an interface for a field, but rename generic names to something more contextual?
s

Shawn

09/13/2018, 6:09 PM
sure
it’s kinda reminiscent of double-brace initializers in Java if you’re familiar with those
h

Hullaballoonatic

09/13/2018, 6:11 PM
i would only ever accident my way onto needing {{}} in java... i would never choose to use them, so I don't really understand them well
if that makes sense... how do I force myself to learn these lambda functions instead of writing excessively verbose code that could be easily shrunk by them?
it's hard to use what you don't really know exists
s

supotuco

09/13/2018, 6:14 PM
The IDE helps a lot
h

Hullaballoonatic

09/13/2018, 6:16 PM
i use intellij when i write in kotlin and it got me to use stuff like
forEach
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...
h

hudsonb

09/13/2018, 6:56 PM
What I do often is provide a
Person(init: () -> Person)
constructor or top-level method so you can do:
val person = Person {
   name = "Tony Stark"
   age = 32
}
😍 1
h

Hullaballoonatic

09/13/2018, 6:56 PM
oh cool
that's a really good idea
s

Shawn

09/13/2018, 6:58 PM
if it’s just parameters, though, why wouldn’t you just used keyword arguments?
h

hudsonb

09/13/2018, 6:59 PM
in the simple Person example, sure
h

Hullaballoonatic

09/13/2018, 6:59 PM
i would normally, but this would be really nice so that I wouldn't have to have massive constructors, or multiple constructors
h

hudsonb

09/13/2018, 7:00 PM
top-level fun can look like
fun person(init: Person.() -> Unit) = Person().apply(init)
h

Hullaballoonatic

09/13/2018, 7:03 PM
I embarrassingly don't really know what you mean by "top-level"
Super class?
Or for factories or something?
i

ilya.gorbunov

09/13/2018, 7:41 PM
"top-level" means the function is located not in some class or object, but right at top level of a file.
b

bkenn

09/13/2018, 7:45 PM
data 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)
}
👍 1
Here is a full example
of using a top level function
h

Hullaballoonatic

09/13/2018, 7:46 PM
oh, i use that often, just didn't know the name for it 😄