atara
09/16/2020, 6:06 AMalso (https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/also.html) and run (https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/run.html). I understand what they are doing but never had the need to use them. Do you use them at all? can you tell a valid case where you used them and it made your code better?PHaroZ
09/16/2020, 6:33 AMalso provide a way to do some side-effect in a fluent style, this could be useful with some old java lib, for example when you have to configure and pass a java.util.Properties (despite in such case I would prefer apply)
aCall(Properties().also {it.put("someProp", "someValue")})
• run : I used it only in rare case, to enhanced code readability when doing some test
val isValid = anObj.anotherObj.aString.run{length>6 && strartWith("foo") && endsWith("bar")}mserralta
09/16/2020, 7:04 AMar p : String? = null
p?.let { println("p is $p") } ?: run { println("p was null. Setting default value to: ")
p = "Kotlin"}
println(p)
//Prints
//p was null. Setting default value to:
//Kotlinmserralta
09/16/2020, 7:04 AMaraqnid
09/16/2020, 9:37 AM.also() I’ve used to do things like logging messages after evaluating something, or for example: pictures.remove(picture)?.also { eventSink.emit(it) }araqnid
09/16/2020, 9:38 AM.run I’ve used with Protobuf builders (from a Java lib): DataType.newBuilder().run { prop1 = value1; prop2 = value2; build() }araqnid
09/16/2020, 9:38 AMlet and apply, I’ve used them very rarelyatara
09/16/2020, 10:58 AMSe7eN
09/16/2020, 10:59 AMstreetsofboston
09/16/2020, 12:30 PMwith (or run): Executing code in a context (eg great for extension functions with multiple receivers)
`apply`: Configuring an object, replacement for builder pattern
`let`: Transform/translate, much like the map function for a List, Set, etc
`also`: For executing side-effects (logging, writing, UI, etc)streetsofboston
09/16/2020, 12:32 PMNir
09/16/2020, 8:42 PMrun is totally unique, since it's the only scope function that's not tied to one specific objectNir
09/16/2020, 8:43 PMrun lambda restricts their scope, prevents people from accidentally using them later.Nir
09/16/2020, 8:45 PMwith exists. It's just an inferior version of member function run; less auto complete friendly and doesn't support ?.Nir
09/16/2020, 8:45 PMstreetsofboston
09/16/2020, 8:46 PMwith(object) for contextual computation (eg multiple receivers). This will work with object.run as well, but in my opinion it seems clearer to read 🙂streetsofboston
09/16/2020, 8:48 PMobject.run where i just want to avoid typing object. too many times 🙂Nir
09/16/2020, 8:49 PMNir
09/16/2020, 8:49 PMNir
09/16/2020, 8:50 PMstreetsofboston
09/16/2020, 8:55 PMwith (obj) { otherObj.hello() }
Run a bunch of methods on object ‘obj’: obj.run { someMethod(); otherMethod(); yetAnotherMethod() }Nir
09/16/2020, 8:56 PMstreetsofboston
09/16/2020, 8:56 PMNir
09/16/2020, 8:56 PMNir
09/16/2020, 8:56 PMstreetsofboston
09/16/2020, 8:56 PMNir
09/16/2020, 8:57 PMstreetsofboston
09/16/2020, 9:00 PMwith function if it were never there 🙂
But the plain, non-extension function, run is a mystery to me. Why run { ... blah ... } or run(lambda), if you can just do … blah … or lambda.invoke()?Nir
09/16/2020, 9:09 PMNir
09/16/2020, 9:09 PMNir
09/16/2020, 9:10 PM.. blah .., the reason as I said before, is you can scope variablesNir
09/16/2020, 9:10 PMNir
09/16/2020, 9:10 PMfoo and bar so that you can do val baz = makeBaz(foo, bar), and for no other reasonNir
09/16/2020, 9:11 PMval baz = run {
// compute foo, compute bar
makeBaz(foo, bar)
}Nir
09/16/2020, 9:11 PMMutableList, but then return from the lambda to a List. So the code outside the run block never is able to mutate the list.Nir
09/16/2020, 9:11 PMfun foo() = run { ... } saves you having to write the type, and effectively gives you type deductionNir
09/16/2020, 9:12 PMstreetsofboston
09/16/2020, 9:19 PMrun { …. } into a new method/function all together. But that can be said for the extension function run as well 🙂Nir
09/16/2020, 9:19 PMNir
09/16/2020, 9:20 PMrun { ... } isn't really an alternative to refactoring. It's an alternative to letting the code sit inline.Nir
09/16/2020, 9:20 PMrun or = other scope functions, to define a function, I really like, it's nice not to have to write the typeNir
09/16/2020, 9:21 PMatara
09/16/2020, 10:01 PM