what is `with` and how do I make use of it?
# getting-started
h
what is
with
and how do I make use of it?
s
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
Copy code
val person = with(Person()) {
      name = "Tony Stark"
      age = 52
      // More such stuff
      this
}
s
from the docs
Calls the specified function
[block]
with the given
[receiver]
as its receiver and returns its result.
h
what is the difference between this and making
person
just an instance of
Person
?
s
if you want to return
this
at the end of the lambda, maybe just use
.apply { ... }
instead?
h
yeah i read the docs, it was just hard to wrap my head around a case in which that is useful
s
with
helps make code look nicer
h
i grabbed the above code from an example i looked up
s
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
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
I still think
Copy code
val person = Person().apply {
  name = "Tony Stark"
  age = 52
}
would be more idiomatic but ¯\_(ツ)_/¯
👍 2
h
like could use that to implement an interface for a field, but rename generic names to something more contextual?
s
sure
it’s kinda reminiscent of double-brace initializers in Java if you’re familiar with those
h
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
The IDE helps a lot
h
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
What I do often is provide a
Person(init: () -> Person)
constructor or top-level method so you can do:
Copy code
val person = Person {
   name = "Tony Stark"
   age = 32
}
😍 1
h
oh cool
that's a really good idea
s
if it’s just parameters, though, why wouldn’t you just used keyword arguments?
h
in the simple Person example, sure
h
i would normally, but this would be really nice so that I wouldn't have to have massive constructors, or multiple constructors
h
top-level fun can look like
fun person(init: Person.() -> Unit) = Person().apply(init)
h
I embarrassingly don't really know what you mean by "top-level"
Super class?
Or for factories or something?
i
"top-level" means the function is located not in some class or object, but right at top level of a file.
b
Copy code
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
oh, i use that often, just didn't know the name for it 😄